I have a project that I created by using Xcode's Single View Application template. Obviously, it comes with a view controller and an app delegate file. Everything works fine. I just wanted to use Xcode's Analyze tool for the first time to make sure everything is fine before submitting to the A开发者_如何学运维pp store. I get the potential leak error for the following lines of code in the app delegate:
self.viewController = [[myViewController alloc] initWithNibName:@"myViewController" bundle:nil];
self.window.rootViewController = self.viewController;
Full app delegate is as follows:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions: (NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[myViewController alloc] initWithNibName:@"myViewController" bundle:nil];
self.window.rootViewController = self.viewController;
[self.window makeKeyAndVisible];
return YES;
}
I didn't modify the app delegate myself. I am using whatever the template gave me. Do I need to release something somewhere in the app delegate? If so, what? and in which method of app delegate?
Nothing needs to be released in the application delegate since the app is terminating and the OS will recover all resources. Indeed, it is improbably that dealloc will even be called.
See SO link for more information.
If you need to do cleanup when the app quits, use applicationWillTerminate:
.
The line
self.viewController = [[myViewController alloc] ...
allocates an instance and then assigns it to the property self.viewController
. On the alloc, the reference count will be 1, but assigning to a property with retain
set will increment the reference count again.
Since the reference count is only decremented by 1 in the dealloc, this object will never be freed = a leak.
See the section on Objective-C memory management in the iOS developer docs for more details.
精彩评论