0

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]];
Chris79
  • 33
  • 7

1 Answers1

1

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

CodeSmile
  • 64,284
  • 20
  • 132
  • 217
  • it can´t be zero.. the first value in is 4. like i said, it gave the value 4 until i updated the project yesterday. – Chris79 Oct 04 '14 at 10:28
  • Have you verified this after the update? The calling code may be affected by the update and now calls this method with 0 for amountOfBricks. Or the error isn't in that code, try adding exception breakpoint to see exactly the line where the error is. EXC_ARITHMETIC and EXC_1386_DIV definitely indicate something like a division by zero error. – CodeSmile Oct 04 '14 at 11:29
  • @Chris79 Regardless, you should guard against amountOfBricks being 0. Also check if you are now building new architectures, surprises lurk there with int, long, long long, unsigned, etc ... – YvesLeBorg Oct 04 '14 at 11:35
  • @LearnCocos2D yes, I´ve verified it. I found out that the issue lies in my if-else statement in the beginning of the init file, that gathers game data from my gamemanager.m file in the beginning of the game, to see if it´s a brand new game or a saved game. I´ve updated my question with the if-else statement – Chris79 Oct 04 '14 at 14:16
  • @YvesLeBorg thanks for the advice, i´ll keep that it mind. For now I think I located the issue, see my updated question. – Chris79 Oct 04 '14 at 14:23