Been trying to solve this error for ages now, hoping that someone here can help! 🙂
I am using Cocos2d with Objective-C and have a simple game. I have just added a ‘restart game’ button that takes you back to the main menu so that you can choose to start a new game. The problem is i am getting EXC_BAD_ACCESS upon re-starting the game.
[self performSelector:@selector(drawone) withObject:nil afterDelay:3];
[self performSelector:@selector(drawtwo) withObject:nil afterDelay:3];
[self performSelector:@selector(drawthree) withObject:nil afterDelay:3];
[self performSelector:@selector(drawfour) withObject:nil afterDelay:3];
[self performSelector:@selector(drawfive) withObject:nil afterDelay:3];
[self performSelector:@selector(drawsix) withObject:nil afterDelay:3];
[self performSelector:@selector(drawseven) withObject:nil afterDelay:3];
[self performSelector:@selector(draweight) withObject:nil afterDelay:3];
[self performSelector:@selector(drawnine) withObject:nil afterDelay:3];
[self performSelector:@selector(drawten) withObject:nil afterDelay:3];
I am running the above code in ‘init’ and this causes the game to crash on the second play through on line
-(void)drawone {
However, if i am to comment out some of the above code e.g
[self performSelector:@selector(drawone) withObject:nil afterDelay:3];
[self performSelector:@selector(drawtwo) withObject:nil afterDelay:3];
[self performSelector:@selector(drawthree) withObject:nil afterDelay:3];
[self performSelector:@selector(drawfour) withObject:nil afterDelay:3];
[self performSelector:@selector(drawfive) withObject:nil afterDelay:3];
/* [self performSelector:@selector(drawsix) withObject:nil afterDelay:3];
[self performSelector:@selector(drawseven) withObject:nil afterDelay:3];
[self performSelector:@selector(draweight) withObject:nil afterDelay:3];
[self performSelector:@selector(drawnine) withObject:nil afterDelay:3]; */
[self performSelector:@selector(drawten) withObject:nil afterDelay:3];
Then i get EXC_BAD_ACCESS on -(void)drawfive this time (still on second play)
Finially, commenting out more of the code e.g
[self performSelector:@selector(drawone) withObject:nil afterDelay:3];
/* [self performSelector:@selector(drawtwo) withObject:nil afterDelay:3];
[self performSelector:@selector(drawthree) withObject:nil afterDelay:3];
[self performSelector:@selector(drawfour) withObject:nil afterDelay:3];
[self performSelector:@selector(drawfive) withObject:nil afterDelay:3];
[self performSelector:@selector(drawsix) withObject:nil afterDelay:3];
[self performSelector:@selector(drawseven) withObject:nil afterDelay:3];
[self performSelector:@selector(draweight) withObject:nil afterDelay:3];
[self performSelector:@selector(drawnine) withObject:nil afterDelay:3]; */
[self performSelector:@selector(drawten) withObject:nil afterDelay:3];
And it makes it to the 6th play through (you can restart it 5 times) before getting EXC_BAD_ACCESS on -(void)drawone
The fact that the EXC_BAD_ACCESS changes places and when it appears leads me to assume that there is not a problem in -(void)drawone since, in some cases, it will run that without problem. I heard that using zombies to debug it could help: after trying unfortunately it did not. I am new to Objective-C so i apologise if this is a simple error. Thank you in advance.
Here is drawone:
-(void)drawone
{
onepos = (arc4random() % 10) + 1;
if(positions[(onepos - 1)] != 0)
{
positions[(onepos - 1)] = 0;
if(onepos == 1)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp(53,420);
[self addChild:onem];
}
else if(onepos == 2)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 1)),420);
[self addChild:onem];
}
else if(onepos == 3)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 2)),420);
[self addChild:onem];
}
else if(onepos == 4)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp(53,300);
[self addChild:onem];
}
else if(onepos == 5)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 1)),300);
[self addChild:onem];
}
else if(onepos == 6)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 2)),300);
[self addChild:onem];
}
else if(onepos == 7)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp(53,180);
[self addChild:onem];
}
else if(onepos == 8)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 1)),180);
[self addChild:onem];
}
else if(onepos == 9)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 2)),180);
[self addChild:onem];
}
else if(onepos == 10)
{
CCMenuItemImage *one = [CCMenuItemImage itemFromNormalImage:@"one.png" selectedImage:@"onepressed.png"
target:self
selector:@selector(oneclick:)];
onem = [CCMenu menuWithItems:one, nil];
onem.positionInPixels = ccp((53 + (106 * 1)),60);
[self addChild:onem];
}
}
else
{
[self drawone];
}
}
and i have global variables:
int positions[] = {1,2,3,4,5,6,7,8,9,10};
int currentclick = 10;
int onepos;
NSTimeInterval starttime;
CCMenu* onem;
CCMenu* twom;
CCMenu* threem;
CCMenu* fourm;
CCMenu* fivem;
CCMenu* sixm;
CCMenu* sevenm;
CCMenu* eightm;
CCMenu* ninem;
CCMenu* tenm;
Again, i am new to objective-c, so am probably doing something really stupid
That may or may not be true. The fact that your code doesn’t always crash doesn’t mean that there’s not a problem. EXC_BAD_ACCESS means that your code is accessing a bad pointer somewhere. It sounds like you’ve got an uninitialized pointer in your code, and your code is crashing whenever that pointer happens to point to an invalid address. If the pointer happens to point to some valid object or contains nil (either of which can easily happen as the result of values left on the stack by other methods) then your code won’t crash.
The way to debug this is to get the code to crash, and then use the debugger to look at the code at the point where the crash happened. Take a look at the variables that are used on the line that caused the exception. If the crash happened in some method that’s not yours (somewhere in a framework) back up the stack looking for the first place where you can see your code and check that instead.