arc4random() stopped working after project update, Thread 1: EXC_ARITHMETIC

146 Views Asked by At

I got a warning on xcode 6.0.1 that I needed to update my project, so I did...

...but then I got issues with arc4random() % instead

Everything was working fine before this update, but now I get the following message: "Thread 1: EXC_ARITHMETIC (code = EXC_1386_DIV, Subcode = 0x0) and the game crashes.

I tried with arc4random_uniform but the game seems to "pause" and nothing happens. What I mean is that I could see that it entered the if(bType == pt || bType == pl) statement but there is seems to "pause" and loop endlessly.

any suggestions?

-(void)placeInGrid:(CGPoint)place pt:(int)pt pl:(int)pl amount:(int)amountOfbricks{

CCLOG(@"BRICKS:placeInGrid");
//CCLOG(@"pt %d", pt);
//CCLOG(@"pl %d", pl);

int bType = arc4random() % amountOfbricks;

//int bType = arc4random_uniform(amountOfbricks); 

if(bType == pt || bType == pl){
    CCLOG(@"bType == pt || bType == pl");
    [self placeInGrid:place pt:pt pl:pl amount:amountOfbricks];
    return;
}
else{
    CCLOG(@"else");
    CCSpriteBatchNode * b = (CCSpriteBatchNode *)[theGame getChildByTag:kSSheet];

    mySprite = [CCSprite spriteWithBatchNode:b rect:[self setBrickType:bType]];

    [b addChild:mySprite z:1];

    self.bricksType =bType; 
    [self.mySprite setPosition:place];

}

}

UPDATE:

the if-else statement below looks for saved data at the beginning of the game, to see if it´s a saved game or a new game.

The problem as it seems, after the project update is that the game thinks that there is already saved data and it goes to the first if statement ( if(gameData)), causing amountofbricks to be equal to 0, instead of going into the else statement where amountofbricks is equal to 4.

I don´t know how to solve this issue.

if ([[GameManager sharedGameManager] isContinuePressed] == NO && [[GameManager sharedGameManager] isNewPressed] == YES) {

        if(gameData){
            CCLOG(@"gameData && isContinuePressed == NO && isNewPressed == YES");
            //Set the local instance of myobject to the object held in the gameState filler with the key "myObject"
            level = 1;
            [[GameManager sharedGameManager] setHScore:[decoder decodeIntegerForKey:@"HighScore"]];
            highscore = [[GameManager sharedGameManager] ReturnHScore];
            countTime = 300;
            AmountOfBricks = [[GameManager sharedGameManager] ReturnAmountOfBricks];
            BeginningOfGame = YES;
            CCLOG(@"AmountOfBricks %d", AmountOfBricks);
        }
        else{
            CCLOG(@"!gameData && isContinuePressed == NO && isNewPressed == YES");

            if([[GameManager sharedGameManager]isThereASavedGame] ==YES){
                CCLOG(@"isThereASavedGame == YES");
                score = 0;
                highscore = [[GameManager sharedGameManager] ReturnHScore];
                level = 1;
                countTime = 300;
                AmountOfBricks = 4;
                BeginningOfGame = YES;
                CCLOG(@"AmountOfBricks %d", AmountOfBricks);
            }
            else{
                CCLOG(@"isThereASavedGame == NO");
                score = 0;
                highscore = 0;
                level = 1;
                countTime = 300;
                AmountOfBricks = 4;
                BeginningOfGame = YES;
                CCLOG(@"AmountOfBricks %d", AmountOfBricks);
            }
        }
    }

UPDATE: I found the issue.

the issue was in my game manager.m file.

NSMutableData *gameData; NSKeyedUnarchiver *decoder = nil;

    NSString *documentPath = [documentsDirectory stringByAppendingPathComponent: @"gameState.dat"];
    gameData = [NSData dataWithContentsOfFile:documentPath];//before the update, I as using this line. was working perfectly. after the update I got a warning on this line "incompatible pointer" and xcode recommended to update to the line below but the line below started to return a zero value. which caused the game to crash.

    //gameData = [NSMutableData dataWithData:[NSData dataWithContentsOfFile:documentPath]];
1

There are 1 best solutions below

5
On

I wager amountOfBricks is 0. You can't modulo by 0 like you can't divide by 0.