If I have a strong reference to a view in my view hierarchy, do I have to set it to nil in viewDidUnload or is it taken care of by ARC?
There is so much misinformation in the two earlier answers I'm going to clarify exactly what the situation is. Note that these are relative to the DEPLOYMENT target, not to whether you use the iOS6 SDK to build):
you don't worry about self.view - the UIViewController manages that
top level objects - ie those NOT contained in another object - must be strong
other objects should use weak (the system nil's the ivar when they are released preventing crashes from trying to use a released object)
for Deployment on pre-iOS6 devices, use viewDidUnload to release top level objects and anything else you want to. NOTE: dealloc is NOT called after this - the next message you get may well be viewDidLoad, when the memory problem that caused the message goes away and the view reappears)
for iOS6 and later, don't use viewDidUnload (see edit below), use didReceiveMemoryWarning if you want to free memory not related to views or the other UI objects.
dealloc is called in all releases. With ARC, you don't call super, and you don't release memory as you did in the old days.
EDIT: From the iOS6 SDK UIViewController class description:
viewDidUnload Called when the controller’s view is released from memory. (Deprecated in iOS 6.0. Views are no longer purged under low-memory conditions and so this method is never called.)
What Apple does do is remove the backing store - the cached pixels. They found this removed most of the consumed memory, and that viewDidUnload was poorly implemented (they told us this at WWDC 2012).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With