UIVIewController not released when view is dismissed

Posted by Nelson Ko on Stack Overflow See other posts from Stack Overflow or by Nelson Ko
Published on 2012-10-23T19:59:12Z Indexed on 2012/10/23 23:00 UTC
Read the original article Hit count: 207

I have a main view, mainWindow, which presents a couple of buttons. Both buttons create a new UIViewController (mapViewController), but one will start a game and the other will resume it. Both buttons are linked via StoryBoard to the same View. They are segued to modal views as I'm not using the NavigationController.

So in a typical game, if a person starts a game, but then goes back to the main menu, he triggers:

[self dismissViewControllerAnimated:YES completion:nil ];

to return to the main menu. I would assume the view controller is released at this point.

The user resumes the game with the second button by opening another instance of mapViewController. What is happening, tho, is some touch events will trigger methods on the original instance (and write status updates to them - therefore invisible to the current view). When I put a breakpoint in the mapViewController code, I can see the instance will be one or the other (one of which should be released).

I have tried putting a delegate to the mainWindow clearing the view:

    [self.delegate clearMapView];

where in the mainWindow

- (void) clearMapView{
    gameWindow = nil;
}

I have also tried

self.view=nil;

in the mapViewController.

The mapViewController code contains MVC code, where the model is static. I wonder if this may prevent ARC from releasing the view.

The model.m contains:

static CanShieldModel *sharedInstance;

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

Another post which may have a lead, but so far not successful, is UIViewController not being released when popped

I have in ViewDidLoad:

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

with the corresponding in ViewDidUnload:

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

Does anyone have any suggestions?

EDIT:

- (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;

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about memory-management