disshModalViewControllerAnimated未释放视图控制器



作为应用程序启动的一部分,我循环使用几个视图控制器。一切正常,但视图控制器不会释放。(永远不会调用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实际上为零。关于如何强迫iOS发布这些未使用的视图控制器,有什么想法吗?

编辑-

解决了!希望这篇文章能帮助其他人——我很困惑。事实证明,我的模态视图控制器是从一个基类派生的,该基类在init方法中设置了观察者(NSNotificationCenter)。必须先移除这些观察者,然后才能释放观察视图控制器。在我的案例中,这变得更加困难,因为观察者是块而不是选择器。当添加这些观察者时,每个观察者都会返回id,因此我必须在NSMutableArray中跟踪这些观察者,并在基类中添加一个释放观察者的方法:

+ (void) safeUnsubscribe:(id)object {
if ( object != nil ) {
    if ( [object isKindOfClass:[CLRemotePrimaryBaseViewController class]]) {
        CLRemotePrimaryBaseViewController* viewController = (CLRemotePrimaryBaseViewController*)object;
        [viewController unsubscribe];
    }        
}

}`

`-(void)句柄:(NSString*)名称usingBlock:(CLObjBlock)块{

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!

`-(无效)取消订阅{

for ( id observer in self.observers ) {
    [[NSNotificationCenter defaultCenter] removeObserver:observer];
}
[self.observers removeAllObjects];
[[NSNotificationCenter defaultCenter] removeObserver:self];

}

已解决!希望这篇文章能帮助其他人——我很困惑。事实证明,我的模态视图控制器是从一个基类派生的,该基类在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];
}

在视图控制器中有尚未释放的对象。为每个视图控制器定义dealloc方法(记得调用[super dealloc]),并放置一个断点来确定是否正在调用。

在每个视图控制器中查找已分配的任何对象,并确保它已被释放。

最新更新