some_id
some_id

Reputation: 29886

Destroying objects on demand with ARC

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

I would like to destroy a number of UIViewControllers 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.

Upvotes: 4

Views: 5859

Answers (3)

Sergey Kalinichenko
Sergey Kalinichenko

Reputation: 726479

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.

Upvotes: 3

SushiGrass Jacob
SushiGrass Jacob

Reputation: 19814

Generally, you can achieve this by setting the object to nil. What's happening behind the scenes is that the object is being released by ARC and then set to nil. This should accomplish what you want.

Upvotes: 1

Ole Begemann
Ole Begemann

Reputation: 135550

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.

Upvotes: 12

Related Questions