Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 387
Thanks Avatar asked Jul 21 '09 12:07

Thanks


1 Answers

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.

like image 167
Sean Patrick Murphy Avatar answered Oct 10 '22 07:10

Sean Patrick Murphy