Search
Goodies

Social Networks
Designs I Like
« Updates | Main | Preparing my application for iOS4 »
Friday
Jun252010

Memory management and viewDidUnload

Here is small guide to UIViewController memory management. A look for "dealloc", "viewDidUnload" on www.stackoverflow.com shows that there is quite some confusions about memory management and the UIViewController class. Let see if I get it myself.

Important methods in viewControllers: viewDidLoad, viewDidUnload and dealloc

loadView: if using Interface Builder, never override this method. Override this method if views are loaded manually.

viewDidLoad: this is called after loadView method. Really useful (needed) when using Interface Builder nibs for  loading other stuff beside the views. "This method is most commonly used to perform additional initialization steps on views that are loaded from nib files." - Apple Documentation.

viewDidUnload: called in low memory conditions. We should unload stuff that we loaded in viewDidLoad method. We need to relinquish ownership of object by calling accessor method to set it to nil. In case of an outlet, the object release itself so the object reference can be set safely to nil. If not a synthesized property, then we first need to release object than we set to nil.

dealloc: release memory used by the receiver of the message. Called in order to cleanup things from the UIViewController unloading.

Taken from www.stackoverflow.com:
"using synthesized methods to set the ivars to nil, which means those ivars are sent release messages. viewDidUnload: is where you're supposed to release any objects or memory you can easily recreate. The author is essentially saying, "I don't need references to these things anymore, decrement the retain count and hopefully that will free up some memory. I'll recreate it later if necessary in viewDidLoad:."

All properties with the IBOutlet should be set to nil in viewDidUnload.

References:

iPhone dev - viewDidUnload subviews

What exactly must I do in viewDidUnload?

Reader Comments

There are no comments for this journal entry. To create a new comment, use the form below.

PostPost a New Comment

Enter your information below to add a new comment.

My response is on my own website »
Author Email (optional):
Author URL (optional):
Post:
 
Some HTML allowed: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong>