Search code examples
iphoneiosdealloc

iOS SDK - dealloc implementation - Release child views first?


I am finishing up an iPad application, and for me, that means filling in all of the dealloc methods in my classes.

I have many views in my nib and most of them contain subviews (i.e. UIViews, UILabels, etc.). Should I release the child views before releasing the parent? If the parent view is released, will its child views be released, as well?

Sample:

-(void)dealloc
{
    [super dealloc];
    [childView release];  // Do I need this if I use the next line?
    [parentView release];  // Will this perform the line above?
}

Both childView and parentView are properties of my view controller. Both have been retained.


Solution

  • Anything that you have retained (whether explicitly or through a retained property) needs to be released for the memory management to be balanced.

    Also, you should invoke [super dealloc] at the end of your dealloc implementation, not the beginning (for instance, because you might depend on superclass resources still being available at the time).