分配rootviewcontroller时内存泄漏

时间:2013-10-09 13:01:36

标签: iphone ios memory-leaks automatic-ref-counting

仪器在self.window.rootViewController= navigationController;显示100%的内存泄漏。应用程序使用ARC。 UINavigationControllerUIViewController和窗口都是属性强的属性。 我该如何解决这个漏洞。

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {    
    txnObserver = [[InAppPurchaseObserver alloc] init];
        txnObserver.delegate = self.viewController;
        [[SKPaymentQueue defaultQueue] addTransactionObserver:txnObserver];

    navigationController = [[UINavigationController alloc]initWithRootViewController:viewController];
        **self.window.rootViewController= navigationController;**
        [self.window makeKeyAndVisible];
        return YES;
}

相同的代码在iOS 6中没有显示任何泄漏,但它在iOS 7中显示泄漏。

2013/10/10更新了有关详情的问题。

1 个答案:

答案 0 :(得分:0)

您可以使用以下代码更改didFinishLaunchingWithOptions而不使用viewController的@property:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    ViewController *viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:rootViewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}

@property (nonatomic, strong) ViewController *viewController;

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    UINavigationController *navigationController = [[UINavigationController alloc] initWithRootViewController:self.viewController];
    self.window.rootViewController = navigationController;
    [self.window makeKeyAndVisible];
    return YES;
}