When should I release objects in -(void)viewDidUnload rather than in -dealloc?

What is the -(void)viewDidUnload is good for?

Could I not just relase everything in -dealloc? If the view did unload, wouldn't -dealloc be called anyway?


Solution 1:

In addition to what has already been indicated, I wanted to elaborate more about logic behind -viewDidUnload.

One of the most important reasons for implementing it is that UIViewController subclasses commonly also contain owning references to various subviews in the view hierarchy. These properties could have been set through IBOutlets when loading from a nib, or programmatically inside -loadView, for instance.

The additional ownership of subviews by the UIViewController means that even when its view is removed from the view hierarchy and released to save memory, through which the subviews are also released by the view, they will not actually be deallocated because the UIViewController itself still contains its own outstanding retaining references to those objects as well. Releasing the UIViewController additional ownership of these objects ensures they will be deallocated as well to free memory.

The objects that you release here are usually recreated and set again when the UIViewController view is re-loaded, either from a Nib or through an implementation of -loadView.

Also note that the UIViewController view property is nil by the time this method is called.

Solution 2:

As the documentation says:

It is called during low-memory conditions when the view controller needs to release its view and any objects associated with that view to free up memory.

In the same situation dealloc is not called. This method is only available in OS3 and above. Dealing with the same situation in iPhone OS 2.x was a real pain!

Update July 2015: It should be noted that viewDidUnload was deprecated in iOS 6 because "Views are no longer purged under low-memory conditions and so this method is never called." So, the modern advice is not to worry about it and use dealloc.

Solution 3:

This is because you will typically set the @property as "(nonatomic, retain)" and as such the setter that is created for you releases the current object and then retains the argument i.e.

self.property = nil;

...does something along the lines of:

[property release];
property = [nil retain];

Therefore you are killing two birds with one stone: memory management (releasing the existing object) and assigning the pointer to nil (since sending any message to a nil pointer will return nil).

Hope that helps.