application:didFinishLaunchingWithOptions: memory management

China☆狼群 提交于 2020-01-14 06:40:28

问题


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?


回答1:


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.




回答2:


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.)



来源:https://stackoverflow.com/questions/4421418/applicationdidfinishlaunchingwithoptions-memory-management

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!