Memory management and viewDidUnload
Friday, June 25, 2010 at 9:10 | tagged
iphonedev,
objective-c 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.
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?
Post a Comment | 

Reader Comments