Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

iPhone - when is dealloc for a viewcontroller called?

Tags:

I am using UINavigationController to move between views. When I move back and forth in views, the memory used by my app keeps on increasing. On placing NSLog statements in the dealloc method of all viewcontrollers, I noticed it was being called for only some viewcontrollers and not all that were popped.

For instance, this is the sequence in which views are pushed MainViewController -> viewcontroller1 -> viewcontroller2 -> viewcontroller3

Now in viewcontroller3 if I do a popToViewController:mainController, dealloc is called only for viewcontroller3. It is not called for viewcontroller1 and 2.

Can someone please tell me why this is happening.

Found the problem. The dealloc method was not being called if any of the references held by a viewcontroller were still in memory. In my case it was the MPMoviePlayerController object which was not released in viewcontroller1 and viewcontroller2.

like image 707
lostInTransit Avatar asked Feb 23 '09 13:02

lostInTransit


People also ask

What is Dealloc?

Deallocates the memory occupied by the receiver.

Why is Deinit not called?

So the deinit is not called if there is no additional scope.


1 Answers

It is a case of last on, first off. So if you imagine your first view controller is the bottom bun of a burger, you then add the burger (second view controller) and then the top burger (third view controller).

If you go back to the burger you take off the top bun (popViewController: calls the dealloc method of the view that is popped). If you want to go back to the bottom bun then you have to pop the burger (dealloc is called every time the view is popped).

not sure why on pop to root the second view controller's dealloc wouldn't be called though..

hmm

like image 60
Dan Morgan Avatar answered Sep 27 '22 17:09

Dan Morgan