【问题标题】:dismissModalViewControllerAnimated not releasing view controllerdismissModalViewControllerAnimated 不释放视图控制器
【发布时间】:2012-02-22 16:38:55
【问题描述】:

在我的应用启动过程中,我会循环浏览多个视图控制器。一切正常,但视图控制器未发布。 (永远不会调用 viewDidUnload 和 dealloc )。在每次状态更改之前,我都会关闭任何呈现的视图控制器。它们被解除,并且为每个视图控制器调用 viewWillDisappear:

- (void)stateChange:(NSString *)state {

// Dismiss any exiting modals/popups
[self dismissPopup];
[self dismissModalViewControllerAnimated: NO];        

if([state isEqualToString:@"StateBoot"]) {

    CLRemotePrimaryBootViewController * viewControllerBoot = [[CLRemotePrimaryBootViewController alloc]initWithNibName: @"CLRemoteBootView" bundle:nil];
    [self presentModalViewController:viewControllerBoot animated:NO];   
    [viewControllerBoot release];
}

else if([state isEqualToString:@"StateLogin"]) {

    CLRemotePrimaryAuthViewController *viewControllerAuth = [[CLRemotePrimaryAuthViewController alloc]initWithNibName: @"CLRemoteAuthView" bundle:nil];
    [self presentModalViewController:viewControllerAuth animated:NO]; 
    [viewControllerAuth release];
}

else if([state isEqualToString:@"StateMain"]) {

    [self setSelectedIndex:0];
}

else if([state isEqualToString:@"StateStop"]) {

    // TBD
}

}

解雇后,self.presentedViewController 实际上是 nil。关于如何强制 iOS 释放这些未使用的视图控制器的任何想法?

编辑-

已解决!希望这篇文章对其他人有所帮助 - 我很困惑。事实证明,我的模态视图控制器是从一个在 init 方法中设置观察者(NSNotificationCenter)的基类派生的。这些观察者必须在观察视图控制器被释放之前被移除。在我的情况下,这变得更加困难,因为观察者是块而不是选择器。添加这些观察者时,每个观察者都会返回 id - 所以我必须在 NSMutableArray 中跟踪这些观察者,并在我的基类中添加一个释放观察者的方法:

+ (void) safeUnsubscribe:(id)object {

if ( object != nil ) {

    if ( [object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) {

        CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object;
        [viewController unsubscribe];
    }        
}

} `

`- (void)handle:(NSString *)name usingBlock:(CLObjBlock)block {

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:^(NSNotification * notification){ block([notification object]); }];
[self.observers addObject:observer];    

}`

// Must call before dealloc or listener will never be released!

`- (void)取消订阅 {

for ( id observer in self.observers ) {

    [[NSNotificationCenter defaultCenter] removeObserver:observer];
}

[self.observers removeAllObjects];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

【问题讨论】:

  • 您应该发布修复作为您自己问题的答案,然后接受它。在深入研究您的问题之前,我不清楚您是否找到了解决方法。

标签: ios


【解决方案1】:

已解决!希望这篇文章对其他人有所帮助 - 我很困惑。事实证明,我的模态视图控制器是从一个在 init 方法中设置观察者(NSNotificationCenter)的基类派生的。这些观察者必须在观察视图控制器被释放之前被移除。在我的情况下,这变得更加困难,因为观察者是块而不是选择器。添加这些观察者时,每个观察者都会返回 id - 所以我必须在 NSMutableArray 中跟踪这些观察者,并在我的基类中添加一个释放观察者的方法:

+ (void) safeUnsubscribe:(id)object {

if ( object != nil ) {
    if ( [object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) {

        CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object;
        [viewController unsubscribe];
    }        
}

} 



- (void)handle:(NSString *)name usingBlock:(CLObjBlock)block {

id observer = [[NSNotificationCenter defaultCenter] addObserverForName:name object:nil queue:nil usingBlock:^(NSNotification * notification){ block([notification object]); }];
[self.observers addObject:observer];    

}


// Must call before dealloc or listener will never be released!

- (void)unsubscribe {

for ( id observer in self.observers ) {

    [[NSNotificationCenter defaultCenter] removeObserver:observer];
}

[self.observers removeAllObjects];

[[NSNotificationCenter defaultCenter] removeObserver:self];

}

【讨论】:

    【解决方案2】:

    在您的视图控制器内部是尚未释放的对象。为每个视图控制器定义dealloc方法(记得调用[super dealloc])并下断点判断是否被调用。

    查看每个视图控制器是否有任何已分配的对象,并确保它已被释放。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-10-04
      • 1970-01-01
      • 1970-01-01
      • 2012-02-13
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多