views:

41

answers:

2

Very basic question about iPhone memory management:

Say I have a viewController with several subviews also controlled by viewControllers. When I remove the top viewController and release the instance - what happens to its children? Are all the therein contained objects released as well?

When I run my app in instruments, I don't get any memory leaks. But the value for «all Allocations» goes up and up? (I assume that this value is the overall memory consumed by my app?)

+1  A: 

It depends where your child UIViewControllers are referenced. If they are only referenced in the root view controller (retained when they are created and released on dealloc), then they will be released when it is deallocated. If you have other references to those view controllers (from your app delegate perhaps?), they will only get released when those references are released.

Cocoa touch NSObjects are reference counted, they are released when their retainCount is decremented to zero. The retainCount is decremented whenever release is called on an object.

Cannonade
A: 

View controllers release their view on dealloc. Views release their subviews on dealloc. A release is not a dealloc.

What's retaining the other view controllers? If your view controller is, then your view controller should release them. Usually this'll be a property, so you can do self.subViewController = nil.

Additionally, if you have any IBOutlets (and I really hope you're using properties for these), you'll also have to set them to nil in dealloc.

Release what you own.

tc.
this lets me a little confused. When I remove a view from a superview using [self.view remove from superview], are the properties of this view removed from the memory or are they still there? Do I have to set the properties to nil before I release them?Seems like I don't get the difference between a released object and one that's dealloc'd...
Swissdude
If more than one thing has retained an object, then release will not dealloc the object. It's only when the retain count drops below one that dealloc is actually done.
Kendall Helmstetter Gelner
@Urs Also important to note that [self.view removeFromSuperview] will decrement the reference count on the view property of your UIViewController, but it won't affect the reference count on the UIViewController itself.
Cannonade
The view should manage its own properties in -[UIView dealloc]. You should manage yours.
tc.