1
votes

I'm building a Cocos2D game in which after two certain sprites collide (by simple bounding box technique), I call

[[CCDirector sharedDirector] replaceScene:gameOverScene];

to change to the game over scene.

Once it initializes the game over scene with everything in it, the game crashes and goes to this method in ccArray.m class:

void ccArrayRemoveAllObjects(ccArray *arr) { while( arr->num > 0 ) CC_ARC_RELEASE(arr->arr[--arr->num]); }

with the message: Thread 1: Program received signal: "EXC_BAD_ACCESS".

I tried to debug using breakpoints and figured out that once my gameOver scene gets initialized and ready to display, the dealloc method in the previous class (the gameplay class, which called the replace scene) gets called and after which it throws this error.

My update method:

-(void)update:(ccTime)dt {

if (CGRectIntersectsRect(plane.boundingBox, enemy.boundingBox)) {
CCScene *gameOverScene = [GameOverLayer sceneWithWon:NO]; [[CCDirector sharedDirector] replaceScene:gameOverScene]; } }

My dealloc method:

- (void) dealloc {

[super dealloc];

[_monsters release];
_monsters = nil;
[mole release];
mole = nil;
[text release];
text = nil;
[mBG1 release];
mBG1 = nil;
[mBG2 release];
mBG2 = nil; }

I have no clue why this is happening. Please help.

Thanking you in anticipation.

2
Can you post your dealloc method of the gameplay class or any relevant code for the crash ?giorashc
@giorashc Please see the question again for the code. I have edited it.Hyder

2 Answers

1
votes

You are calling super's dealloc before you are deallocating your gameplay scene. Call super's dealloc at the end of your dealloc method :

- (void) dealloc {   
   [_monsters release];
   _monsters = nil;
   [mole release];
   mole = nil;
   [text release];
   text = nil;
   [mBG1 release];
   mBG1 = nil;
   [mBG2 release];
   mBG2 = nil; 

   [super dealloc];
}

When you are calling super's dealloc first you are releasing your instance making it members invalid so accessing them after the dealloc will create memory bad access issues

1
votes

because you didn't show enough code,i can only guess that some of these objects,such as _monsters,mole,text,mBG1,mBG2,were autoreleased before you release them in the dealloc funtion.So,they can not be released anymore.This is concerned ARC-automaic reference counting which you should know, it's basis of ios. try this only:

- (void) dealloc {

    [super dealloc];

}