views:

62

answers:

1

Hi Friends,

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

+2  A: 

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 viewDidUnload is 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 in viewDidLoad (because viewDidLoad will be called again when the view controller need to recreate its view). This includes all your outlets. You also have to set these variables to nil to avoid overreleasing them.

Because in dealloc, you should release all objects your view controller retains, including those you included in viewDidUnload.

Have a look at any of Apple's sample code to see how Apple does this.

Ole Begemann
Hi Ole, Thanks for the response.Now I understand that, I should release in ViewDidUnload, what ever I have created in ViewDIdLoad.One doubt, I have is that should I release something in -dealloc method that already I have released in -ViewDidUnload method. Because When my -dealloc is to be called, the view should be removed and the -viewDidUnload must have been called. So I guess it would be a duplication of code.Please share your thoughts. Thanks Regards,krishnan.
Krishnan
Apple has this duplicate release pattern in all their sample code so I suggest you follow their example. It is not entirely clear from the documentation whether `viewDidUnload` is always called before `dealloc`. It is important to also set the variables to `nil` in `viewDidUnload` to avoid over-releasing objects.
Ole Begemann
ok Thanks Ole Begemann. I will use whatever Apple follows.
Krishnan
This just may add a little -- from experience viewDidUnload is rarely called. In fact, it is only called when the app experiences a low memory scenario, from the apple docs on viewDidUnload. If you want to be sure you've freed up memory, then dealloc is the place to do it. Duplication shouldn't matter -- you can send messages (like release) to nil without a problem. But make sure you nil out your variables otherwise you'll be sending messages to a random chunk of memory possibly!
Andrew