If I have an NSArray of UIViewControllers and send release to the array, will that call viewDidUnload or dealloc for each of the UIViewControllers? or neither?
Here’s what I’m doing:
- (void) viewDidLoad {
UIViewController* profileController = [[ProfileController alloc] init];
..........
//all the other controllers get allocated same way
self.viewControllers = [[NSMutableArray alloc] initWithObjects: profileController, dietController, exerciseController, progressController, friendsController, nil];
[profileController release];
//other controllers get released same way ....
}
- (void) dealloc {
[viewControllers release];
NSLog("DEALLOC!");
//I know dealloc is being called
//what happens to the view controllers?
}
I put a breakpoint in the viewDidUnload and dealloc methods for each of these view controllers, and they don’t get called.
As pgb mentioned, you are confusing the two concepts of memory management and view controller life cycle.
viewDidUnloadwill be called whenever the view controllers view is unloaded, which of course will only happen if the view is loaded. In the absence of displaying any of the view controllers you should not expect to seeviewDidUnloadcalled at all.You very much should expect to see
dealloccalled though!Your problem is with the initialisation of the
viewControllersarray:Assuming that
viewControllersis a property that is using the retain attribute, this will leak the array, and thus all the array’s contents. The problem is that you have alloc’d a mutable array (thus retain count = 1), then you are assigning it to the viewControllers property which will increment the retain count.In your dealloc method you (correctly) release the array, but this will merely decrement the retain count to one.
My suggested fix is to add
autoreleaseto the above code:After making this change you should expect to see
deallocbeing called on the view controllers. You should also expect to seeviewDidUnloadcalled as the views of these view controllers are unloaded (for example, as they are popped off of the stack in a navigation-controller based application).