iphone - 推送 View Controller : Is this a memory leak?

标签 iphone objective-c ios memory-leaks

我正在使用最新的 SDK 和 XCode 4.2 开发 iOS 4(我使用ARC)。

我正在以编程方式开发导航 Controller ,我有一个问题。

这是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]

我已经分析了该应用程序,并且没有发现任何内存泄漏。但我必须在这里问,因为我不确定。

最佳答案

如果再次调用showSecondViewController方法,将会出现内存泄漏。

您应该在 showSecondViewController 方法中释放 secondViewController

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

当您执行 pushViewController:secondViewController

时,它会自动被 navController 保留

关于iphone - 推送 View Controller : Is this a memory leak?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8677490/

相关文章:

iphone - iPhone iOS 上的锁屏广告应用 Apple iAds 指南

iphone - iPhone 上的并发后台下载

iOS - GCD 和 __strong 引用

ios - 创建在 iOS 邮件应用程序中正确显示的内联图像

ios - 如何从 REST API search.list 结果中过滤掉 YouTube 的内容屏蔽视频

ios - 证书过期后,是否有必要在苹果创建一个新的基于推送通知的配置文件

iphone - 您可以更改在 iOS 的 safari 中更改方向时显示的内容吗?

objective-c - 如何设置 NSOpenPanel 对话框的位置和大小?

objective-c - 多次用新数据刷新 NSTableView

iphone - 如何使UIPickerView像UIDatePicker一样?