简体   繁体   English

为什么此代码会导致内存泄漏?

[英]Why does this code cause a memory leak?

I execute this code in my applicationDidBecomeActive callback. 我在applicationDidBecomeActive回调中执行此代码。

- (void)applicationDidBecomeActive:(UIApplication *)application{
     [self showMainWindow];
}


- (void) showMainWindow{
      PushNotificationTabBarController *pushNotificationTabBarController = [[PushNotificationTabBarController alloc] initWithNibName:@"PushNotificationTabBarController" bundle:nil];
      self.viewDeckController.centerController = 
          pushNotificationTabBarController; // registered for Notification via addObserver...
}

The setter is defined like this: setter的定义如下:

@property (nonatomic, strong) IBOutlet UIViewController* centerController;

IIViewDeckController property in my AppDelegate : 我的AppDelegate IIViewDeckController属性:

@property (strong, nonatomic) IIViewDeckController *viewDeckController;

I found out that I have 2 PushNotificationTabBarController objects when I close and re-enter the app, with no reference to one of them, like a memory leak. 我发现当我关闭并重新进入应用程序时,我有2个PushNotificationTabBarController对象,没有对其中之一的引用,例如内存泄漏。 However I dont understand why this code is causing it because to me it seems ok. 但是我不明白为什么这段代码会导致它,因为对我来说似乎还可以。 I'm using ARC . 我正在使用ARC

Before setting pushNotificationTabBarController check for self.viewDeckController.centerController object. 在设置pushNotificationTabBarController之前,请检查self.viewDeckController.centerController对象。 If it exists, release and assigned to nil and assign new object of 'pushNotificationTabBarController' 如果存在,则释放并分配给nil并分配'pushNotificationTabBarController'的新对象

 id controller = self.viewDeckController.centerController;
    if (controller) {
 [controller release];
  controller = nil;
}

  self.viewDeckController.centerController = pushNotificationTabBarController;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM