24

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.

lostInTransit
  • 70,519
  • 61
  • 198
  • 274
  • I encountered exactly the same problem; dealloc is called only for the 3rd controller. You said "The dealloc method was not being called if any of the references held by a viewcontroller were still in memory." Can you elaborate on this? – Wayne Lo Jun 10 '11 at 02:40
  • 1
    The viewcontroller was a delegate of an object still being used. I removed it from being the delegate and then it worked fine. Was not getting dealloced since the other object held its reference (as delegate) – lostInTransit Jun 10 '11 at 04:07

4 Answers4

13

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

Dan Morgan
  • 2,500
  • 5
  • 25
  • 32
  • Thanks Dan. Thats what is surprising me that dealloc is called for some classes and not for others. Are you aware of any situations when this could happen? – lostInTransit Feb 23 '09 at 14:58
  • I am facing the same problem i also have two viewcontrollers and second viewcontroller's dealloc is not getting called when pop from navigation stack. – Heena Feb 22 '13 at 06:13
0

You should also keep in mind that dealloc is not always guaranteed to run.

Oscar Gomez
  • 18,436
  • 13
  • 85
  • 118
  • 1
    That is incorrect. Dealloc will run when the last reference to an object has been released. – Jason Nov 12 '09 at 15:29
  • 6
    Well not completely incorrect, this is true if the application is exiting according to the Memory Management Programming Guide for Cocoa. – Oscar Gomez Dec 10 '09 at 22:48
0

where you navigate your controllers there should be need to write this line after pushviewcontroller, [viewControllername release]; then it would be call each viewcontroller Dealloc method :)

  • This is not the actual answer to the question asked, more of a 'point to remember' for new ObjC devs. – Sushant Feb 03 '11 at 09:23
-1

use like [self.navigationController popToRootViewControllerAnimated:YES]; It worked for me... calls all dealloc methos of my 10 view controllers