When we need to release an object,where to do it, either at the dealloc method or in the ViewDidUnload for a viewController? which would be better?
I think that viewDidUnload would do better, because once the view controller is unloaded the objects would be released. And in the dealloc case, from the documentation
When an application terminates,
objects may not be sent a dealloc
message. Because the process’s memory
is automatically cleared on exit, it
is more efficient simply to allow the
operating system to clean up resources
than to invoke all the memory
management methods.
the objects may not be sent a dealloc message upon the Application quitting.
So I find it would be better if objects got released earlier, than postponing their release thereby reducing the memory footprint the Application takes.
If my understanding is wrong, please correct me.
Thanks and Regards,
Krishnan
Yes, your understanding is wrong.
First, it doesn’t matter what happens when the application quits because the OS will free all the memory that your app allocated anyway. Not calling dealloc on app termination is just Apple’s way to speed this up.
Second, it is not the view controller that is unloaded when
viewDidUnloadis called but only its view. The view controller stays alive until it is deallocated.In
viewDidUnload, you have to release those objects that are part of the view and everything that can and will be recreated inviewDidLoad(becauseviewDidLoadwill be called again when the view controller need to recreate its view). This includes all your outlets. You also have to set these variables tonilto avoid overreleasing them.Because in
dealloc, you should release all objects your view controller retains, including those you included inviewDidUnload.Have a look at any of Apple’s sample code to see how Apple does this.