application:didFinishLaunchingWithOptions: memory management
I have a question about the memory management. In my app delegate, I have the following method; where welcomeViewController
is an ivar.
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
welcomeViewController = [[CBWelcomeViewController alloc] init];
UINavigationController *appNavigationController = [[UINavigationController alloc] initWithRootViewController:welcomeViewController];
[self.window addSubview: [appNavigationController view]];
[self.window makeKeyAndVisible];
return YES;
}
To release the memory for welcomeViewController
, I simply call release on it in the dealloc method.
- (void)dealloc {
[welcomeViewController release];开发者_如何学编程
[window release];
[super dealloc];
}
My question is, what is the correct way to manage the memory of appNavigationController
?
You should make appNavigationController
an instance variable and release
it in dealloc
.
You do not need to have welcomeViewController
as an instance variable, quite the opposite.
Simply alloc/init it, then pass it off to the UINavigationController
, which then retains it, then immediately release
it.
You need to release it within the dealloc method, like you're currently releasing the welcomeViewController.
(Incidentally, you should actually release the welcomeViewController straight after you've used it to init the navigation controller (i.e.: within your init method), as the navigation controller will retain it.)
精彩评论