当视图被解除时,UIVIewController未释放

时间:2012-10-23 19:59:12

标签: objective-c memory-management uiviewcontroller automatic-ref-counting

我有一个主视图,mainWindow,它提供了几个按钮。这两个按钮都创建了一个新的UIViewController(mapViewController),但是一个将启动一个游戏,另一个将恢复它。两个按钮都通过StoryBoard链接到同一个视图。因为我没有使用NavigationController,所以它们被视为模态视图。

所以在一个典型的游戏中,如果一个人开始游戏,然后回到主菜单,他会触发:

[self dismissViewControllerAnimated:YES completion:nil ];

返回主菜单。我认为视点控制器此时已被释放。

用户通过打开另一个mapViewController实例,使用第二个按钮恢复游戏。发生了什么,是一些触摸事件将触发原始实例上的方法(并向其写入状态更新 - 因此对当前视图不可见)。当我在mapViewController代码中放置断点时,我可以看到实例将是一个或另一个(其中一个应该被释放)。

我已经尝试将一个委托给mainWindow清除视图:

    [self.delegate clearMapView];

在mainWindow中的位置

- (void) clearMapView{
    gameWindow = nil;
}

我也试过

self.view=nil;
mapViewController中的

mapViewController代码包含MVC代码,其中模型是静态的。我想知道这是否会阻止ARC发布视图。

model.m包含:

static CanShieldModel *sharedInstance;

+ (CanShieldModel *) sharedModel
{
@synchronized(self)
{
    if (!sharedInstance)
        sharedInstance = [[CanShieldModel alloc] init];     
    return sharedInstance;
}
return sharedInstance;
}

另一篇可能具有领先优势但迄今未成功的帖子是UIViewController not being released when popped

我在ViewDidLoad中:

    // checks to see if app goes inactive - saves.
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(resignActive) name:UIApplicationWillResignActiveNotification object:nil];

与ViewDidUnload中的对应:

[[NSNotificationCenter defaultCenter] removeObserver:self name:UIApplicationWillResignActiveNotification object:nil];

有人有任何建议吗?

编辑:

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
NSString *identifier = segue.identifier;

if ([identifier isEqualToString: @"Start Game"]){
    gameWindow = (ViewController *)[segue destinationViewController];
    gameWindow.newgame=-1;
    gameWindow.delegate = self;

} else if ([identifier isEqualToString: @"Resume Game"]){
    gameWindow = (ViewController *)[segue destinationViewController];
    gameWindow.newgame=0;
    gameWindow.delegate = self;

1 个答案:

答案 0 :(得分:0)

我的理论是,某些内容强烈引用了您的UIViewController

根据您在上面发布的segue代码,您似乎正在为模态视图控制器保留引用gameWindow。如果是这种情况,那么您需要在其声明前添加__weak,以便在您解除它时不会保留mapViewController