Reputation: 53
I have a navigation based application and I see a leak in the applicationDidFinishLaunchingWithOptions
method in the app delegate. I am not sure where to release the viewController
.
Header file:
@class ViewController;
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
ViewController *viewController;
}
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) ViewController *viewController;
@end
.m File
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
UINavigationController *nvc = [[UINavigationController alloc] initWithRootViewController:viewController];
nvc.navigationBar.tintColor = [UIColor grayColor];
nvc.navigationBar.barStyle = UIBarStyleDefault;
[window addSubview:[nvc view]];
[window makeKeyAndVisible];
return YES;
}
- (void)dealloc {
[viewController release];
[window release];
[super dealloc];
}
Adding the [nvc release]
after the windows addSubview
line makes my viewcontroller
disappear .Adding autorelease
to the alloc
line freezes the app and cannot redirect to the next view. I would like to fix all leaks before I submit the app to the app store. Can anyone please tell me what is the correct place to release the rootViewController
.
Thank You.
Upvotes: 0
Views: 96
Reputation: 17143
You can just keep the navigation controller in a @property of the app's delegate, instead of the viewcontroller. So this would work:
Header file:
#import <UIKit/UIKit.h>
@interface AppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *window;
UINavigationController *navController;
}
// if using current compiler, just omit these ivars above
@property (nonatomic, retain) UIWindow *window;
@property (nonatomic, retain) UINavigationController *navController;
@end
Then @synthesize as usual and release the backing instance variable in your dealloc.
BTW, if you do not need to support iOS3, then don't add the nav controller's view as a subview, just set the window's rootviewcontroller instead.
So do this:
self.window.rootViewController = self.navController;
instead of:
[window addSubview:[nvc view]];
Then of course creating your navigation controller becomes:
self.navController = [[[UINavigationController alloc] initWithRootViewController:viewController] autorelease];
[viewController release];
Assuming you just created 'viewcontroller' earlier (you didn't show that)
Upvotes: 1
Reputation: 6172
i could be wrong but i believe that normally rootviewcontroller usually is like the delegate meaning its never released till the application is closed. however to answer your question the proper place to release most things is in the dealloc method (just add it if its not there, it will be called)
-(void)dealloc
{
[varName release];
}
post saying simliar things as me: http://www.iphonedevsdk.com/forum/iphone-sdk-development/5953-navigationcontroller-appdelegate-crash.html
couldnt find an official link but im pretty sure im right
Upvotes: 0