2011-12-30 90 views
0

我正在開發iOS 4的最新SDK和XCode 4.2(我是而不是使用ARC)。PushViewController:這是內存泄漏嗎?

我正在開發一個導航控制器編程,我有一個問題。

這是AppDelegate.h

#import <UIKit/UIKit.h> 

@class ViewController; 
@class SecondViewController; 

@interface AppDelegate : UIResponder <UIApplicationDelegate> 
{ 
    UINavigationController* navController; 
    ViewController* viewController; 
    SecondViewController* secondViewController; 
} 

@property (strong, nonatomic) UIWindow *window; 

- (void) showSecondViewController; 

@end 

這是AppDelegate.m

#import "AppDelegate.h" 

    #import "ViewController.h" 
    #import "SecondViewController.h" 

    @implementation AppDelegate 

    @synthesize window = _window; 

    - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions 
    { 
     self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]]; 

     viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil]; 
     viewController.title = @"Menu"; 
     navController = [[UINavigationController alloc] initWithRootViewController:viewController]; 
     navController.navigationBar.tintColor = [UIColor blackColor]; 
     self.window.rootViewController = navController; 

     [self.window makeKeyAndVisible]; 
     return YES; 
    } 

- (void) dealloc 
{ 
    [_window release]; 
    [viewController release]; 
    [navController release]; 
    [secondViewController release]; 
} 
    - (void)applicationWillResignActive:(UIApplication *)application 
    { 
     ... 
    } 

    - (void)applicationDidEnterBackground:(UIApplication *)application 
    { 
     ... 
    } 

    - (void)applicationWillEnterForeground:(UIApplication *)application 
    { 
     ... 
    } 

    - (void)applicationDidBecomeActive:(UIApplication *)application 
    { 
     ... 
    } 

    - (void)applicationWillTerminate:(UIApplication *)application 
    { 
     ... 
    } 

    - (void) showSecondViewController 
    { 
     secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
     secondViewController.title = @"Second"; 
     [navController pushViewController:secondViewController animated:YES]; 
    } 

我的問題是關於最後一個方法,-(void)showSecondViewController;

我可以在添加此行結束?

[secondViewController release] 

我已經成型的應用,我還沒有看到任何內存泄漏。但我必須在這裏問,因爲我不確定。

回答

1

如果再次調用showSecondViewController方法,將會發生內存泄漏。

您應該在showSecondViewController方法中釋放secondViewController

- (void) showSecondViewController 
{ 
    secondViewController = [[SecondViewController alloc] initWithNibName:@"SecondViewController" bundle:nil]; 
    secondViewController.title = @"Second"; 
    [navController pushViewController:secondViewController animated:YES]; 
    [secondViewController release] 
} 

它會自動navController被保留,當你做pushViewController:secondViewController