What is the correct way to destroy an object with ARC?
I would like to destroy a number of UIViewController
s as well as an object holding an AUGraph at certain times during runtime.
At the moment, when my parent viewcontroller creates viewcontroller objects and assigns their views to its view
, the objects obviously stay alive with the parent. I would like to destroy these child viewcontrollers the moment they are not needed.
Just set the variables referencing those objects to nil
. The compiler will then release the objects at that moment and they will be destroyed if no other strong references to them exist.
ARC will insert a call to [release]
when you set a __strong
variable that references an object to nil
.
@interface MyViewController : UIViewController {
UIViewController *childViewController;
}
...
@end
-(void)destroyChild {
childViewController = nil;
}
The same thing is done when you have a C-style array of objects: setting an element of your array to nil
releases the item that was there unless it was __weak
/__unsafe_unretained
. If you keep child view controllers in an NSMutableArray
, removing an object from the array decrements its reference count.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With