Destruction of objects on demand using ARC

What is the correct way to destroy an object using ARC?

I would like to destroy several UIViewController , as well as an object containing an AUGraph at a specific time at runtime.

At the moment, when my parent viewcontroller creates the viewcontroller objects and assigns their views to its view , the objects obviously remain alive with the parent. I would like to destroy these child view controllers at a time when they are not needed.

+4
source share
3 answers

Just set the variables that reference these objects to nil . Then the compiler will free the objects at that moment, and they will be destroyed if no other strong references to them are found.

+12
source

ARC inserts a [release] call when you set the __strong variable, which references the object to nil .

 @interface MyViewController : UIViewController { UIViewController *childViewController; } ... @end -(void)destroyChild { childViewController = nil; } 

The same thing happens when you have an array of C-style objects: setting the element of your array to nil frees the element that was there if it was not __weak / __unsafe_unretained . If you store child controllers in an NSMutableArray , removing an object from the array reduces its number of references.

+3
source

Typically, you can achieve this by setting the object to zero. What happens behind the scenes is that the object is released by ARC and then set to zero. This should do what you want.

+1
source

All Articles