views:

72

answers:

3

I am developing an iPhone application which is navigation based. Whenever the application quits, the retain count of the navigation controller and the window is 3. Can somebody explain me how to overcome this issue? The dealloc method, as a result, is not getting called.

+2  A: 

There is no issue.

  • You should try not to concern your self with retain counts unless you see a definitive leak (usually in Instruments)
  • You don't need to worry about objects like your window (and, probably, your nav controller) being dealloc'd at application quit-time; ALL of your app's memory will go away at that time.
Ben Gottlieb
But none of the dealloc methods, neither for the UIViewController's nor the Appdelegate's are being called. The retaincount values of the view controllers are 1. The problem is, I retain them in a rootviewcontroller as they would be used again and again, and it would be lame to release them and allocate them again. So, when do I release these other UIViewControllers? The dealloc method of the rootviewcontroller is not being called.
Lakshmie
There's no guarantee that your app delegate's dealloc method will ever get called. Thus, if it's responsible for releasing your nav controller (which, in turn retains all your visible view controllers), none of that stuff will ever be dealloc'd. If you're theoretically releasing and then re-creating stuff while the app is running, and not seeing your dealloc called, then you have an unbalanced -retain somehwere.
Ben Gottlieb
That makes sense. So, if during the application run if I release an object, its dealloc method would be called (assuming that the retains are balanced), but the dealloc methods would not be called if the user quits the application. In this case, the heap space for the application is simply flushed. is this right?
Lakshmie
That is correct; on quit there is no sense in trying to deallocate memory as the entire app's memory is going to be handed back to the system all at once with a low level, and very efficient, operation.
bbum
A: 

But none of the dealloc methods, neither for the UIViewController's nor the Appdelegate's are being called. The retaincount values of the view controllers are 1. The problem is, I retain them in a rootviewcontroller as they would be used again and again, and it would be lame to release them and allocate them again. So, when do I release these other UIViewControllers? The dealloc method of the rootviewcontroller is not being called.

Lakshmie
A: 

You overcome this issue by not depending on dealloc methods being called. Application teardown code should go in the appropriate application methods. An object's dealloc should just do what's necessary to release that object's memory and fulfill its part in the memory management contract. When your app is terminated, all its memory is freed, so there's no need for dealloc to be called.

Chuck
Ok. In the tutorials that I have looked into, I was always educated that if I release the memory of an object, its dealloc method would be called. Are you suggesting that this is incorrect?
Lakshmie
ok.. so the dealloc method of the rootviewcontroller will never be called?
Lakshmie
Releasing an object does not necessarily make it dealloc. It will usually dealloc when there are no references to it (or it might not — some objects live forever, like constant strings). And yes, objects that exist for the entire life of your application will probably never execute their dealloc methods.
Chuck