5
votes

I am working with Automatic Reference Counting. I have a custom UIViewController subclass and whenever I call -presentViewController: animated:completion: or remove its view from the superview I would like to NSLog something like "I am dealloced" so I know that the view controller has successfully been removed. I have implemented the -dealloc method in my view controller. However I started a test project where I just had two UIViewController instances (no retain cycles) and -dealloc is not called either when I push the second UIViewController modally or when I remove the superview or when I remove it from the parent view controller. Am I missing something ? In my original project (not the test case) Instruments shows me that those controllers leave a memory footprint that I can't get rid off.

4
@ThilinaHewagama -viewDidDisappear: does work, but does this method indicate deallocation ? - the_critic
I have never had NSZombieEnabled on and I think this issue is resolved in iOS6. - the_critic
It's not at all clear from what you posted at Github what you're trying to do. Removing a controller's view from the window doesn't deallocate the controller. You should post here, an example of what you're doing -- show the whole code for the controller you think should be deallocated. It helps to see it in context. - rdelmar
@rdelmar I thought removing the controller's view would deallocate the controller as well... But yeah I am trying to actually get rid of the whole controller not just the view, so that the view controller gets dealloced. - the_critic
How you do that depends a lot on your structure. Dismissing a presented controller, or popping a controller off a navigation stack both should cause deallocation. Also, if you just switch the root view controller of the window, and yours is the one you're switching away from, that will also cause it to be deallocated. - rdelmar

4 Answers

11
votes

If you want to switch view controllers, and have the one you're switching away from be deallocated, then just switch the root view controller of the window. So, if you're in VC1 and want to go to VC2, then do this in VC1:

VC2 *vc2 = [[VC2 alloc] init]; // or however else is appropriate to get an instance of this class
self.view.window.rootViewController = vc2;

If you haven't created any property to point to vc1, then it will be deallocated after making this switch.

If you want to use a modal presentation or a modal segue (to get the animation when you switch controllers), you can still get the initial controller to be deallocated by switching the root view controller after the presentation from the viewDidAppear method of vc2:

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:animated];
    self.view.window.rootViewController = self;
}
0
votes

To get a print when the View Controller is deallocated you can implement the dealloc method as

- (void) dealloc {
    NSLog(@"The instance of MyViewController was deallocated");
}

Then to get a print when the View Controller left the view you can implement

- (void) viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    NSLog(@"The instance of MyViewController left the main view")
}
0
votes

If you use -presentViewController:animated:completion: you are retaining the parentViewController every time you call this method. ModalViewControllers are simply pushed on top of the other ViewController.

ModalViewControllers are only designed for some kind of information / User Input and stuff like that. If you want to dealloc the ParentViewController you have to deal with your own implementation.

0
votes

dealloc method isn't called when the class is retained (or something in this class is retained) and not reeleased. It is justly for projects with both ARC and without it. So check your code twice.