views:

981

answers:

4

I tend to release my stuff in -dealloc, and now iPhone OS 3.0 introduced this funny -viewDidUnload method, where they say:

// Release any retained subviews of the main view. // e.g. self.myOutlet = nil;

So -viewDidUnload seems to get called when the view of the view controller has been kicked off from memory. And if I have subviews attached to the main view of the view controller, I have to release that stuff only HERE, but not in -dealloc as well?

That's confusing. Also, what if -dealloc causes the view to be unloaded (released)? Then again, it will call -viewDidUnload?

I do realize the difference, that -viewDidUnload is just for the case where the view itself gets killed, but the view controller stays in memory. And -dealloc is for the case where the whole thing goes to trash.

Maybe someone can clear up the confusion.

+10  A: 

The intent here is to "balance out" your subview management. Anything that you create in viewDidLoad should be released in viewDidUnload. This makes it easier to keep track of what should be released where. In most cases, your dealloc method is a mirror-image of your init method, and your viewDidUnload will be a mirror image of your viewDidLoad method.

As you pointed out, the viewDid... methods are to be used when the view itself is loaded and unloaded. This permits a usage pattern in which the view controller remains loaded in memory, but the view itself can be loaded and unloaded as required:

init
viewDidLoad
viewDidUnload
viewDidLoad
viewDidUnload
...
dealloc

Of course, it doesn't hurt to release things in your dealloc method as well, as long as you set them to nil when you release them in viewDidUnload.

The following quote from the Memory Management section of Apple's UIViewController documentation, describes it in more detail:

...in iPhone OS 3.0 and later, the viewDidUnload method may be a more appropriate place for most needs.

When a low-memory warning occurs, the UIViewController class purges its views if it knows it can reload or recreate them again later. If this happens, it also calls the viewDidUnload method to give your code a chance to relinquish ownership of any objects that are associated with your view hierarchy, including objects loaded with the nib file, objects created in your viewDidLoad method, and objects created lazily at runtime and added to the view hierarchy. Typically, if your view controller contains outlets (properties or raw variables that contain the IBOutlet keyword), you should use the viewDidUnload method to relinquish ownership of those outlets or any other view-related data that you no longer need.

e.James
viewDidUnload isn't a mirror of viewDidLoad since the Unload method sets outlets to nil.
Arne Evertsson
Which Unload method are you referring to? I took a peek through the UIViewController documentation, but I didn't find a method with that name. When I described viewDidUnload as being (usually) a mirror image of viewDidLoad, I meant in terms of memory management; i.e.: anything allocated, copied or retained in viewDidLoad should be released in viewDidUnload. IBOutlets are usually configured automagically when your nib file is loaded, so even if they are set to nil prior to the call to viewDidUnload, that shouldn't affect anything from a memory standpoint.
e.James
To be clear; I can see how it would be *possible* to run into trouble if you were to allocate an object in viewDidLoad which would become inaccessible in viewDidUnload when all of the IBOutlets had been set to nil. This would be a strong indication of bad design, however, since an object should always maintain a pointer to any memory that it is responsible for releasing.
e.James
Amazing answer!
Typeoneerror
@Typeoneerror: Thank you for saying so `:)`
e.James
+2  A: 

As you say viewDidUnload will be called if self.view=nil, this generally occurs if you get memory warning. In this method you must release any subview of the mainview which can easily be created by .xib or loadView method. You should release any data object if you create them in viewDidload or loadView etc. because these methods will be called again to present view to the user, those data can be recreated easily.

EEE
A: 

When you get a memory warning usually the viewcontroller will unload it's view but itself will not be dealloc.
All that can be re-created easily should be unloaded, but not the model of the view.

CiNN