简体   繁体   中英

viewWillDisappear Causes App to Crash on Device but not on Simulator

I am developing a simple game on Xcode 4.4 for iOS 5.1 using storyboards, ARC, and a navigation controller. The app works perfectly on the simulator, but not on the device (iPhone 4 CDMA). So basically, I have a main menu with 3 UIButtons (Play Game, Options, Help). When I click on Play Game and then try to go back to the menu via the navigation controller back button, the app crashes on the device. It is stopped at the following thread:

Thread 1: EXC_BAD_ACCESS (code=1, address=0x70000008)

and pointed to the following:

0x35b4df78:  ldr    r3, [r4, #8] 

There is also a point in my code where I am calling the popToRootViewContoller method. It also crashes here (with same thread error as I would've thought). However, if I comment out the viewWillDisappear method, then I am able to switch back and forth with no issue. The Options and Help screen do not implement the viewWillDisappear method and switch back and forth perfectly on the device.

I have the following under the viewWillDisappear method:

-(void)viewWillDisappear:(BOOL)animated
{
    [tmrCountdown invalidate];
    [tmrEclapsedTime invalidate];
    [tmrMainEnemyMovement invalidate];
    [tmrMoveSpawnedEnemies invalidate];
    [tmrSpawnEnemies invalidate];
    accInc=currPrefs.accelerometerSensitivity;
    enemySpeedX=5.0;
    enemySpeedY=5.0;
    countdown=4;
    ecMiliseconds=0;
    randTime=0;
    stopped=NO;
    gameStarted=NO;
}

I call the popToRoot method here:

-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex
{
    if (buttonIndex==0)//cancel
    {
         //called here
        [self.navigationController popToRootViewControllerAnimated:YES];
    }
    else //1 (Play Again)
    {
        [self reInit];
    }
}

Thanks, Mehul

As I see from code, you are invalidating timers, and this may occur if you try to invalidate an invalid( a non repeating timer

repeats:NO

or released timer. It is not an exception, so you cant catch it with @try block. It is a signal. What you have to do is:

In your timer selector you need to call

[timer release]; // if you have allocated it
timer=nil;
...
Some action

And in your viewWillDisappear

If (timer!=nil) { 
    [timer invalidate];
    timer=nil;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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