This problem has been confusing me for days now. I have an NSString, ‘spriteType’. It is declared using the property and synthesise method. In my layer, where the string is created, I also create a CCNode. The node is created in a method.
- (void) spritePick {
CCMenuItemImage *go = [CCMenuItemImage itemFromNormalImage:@"button_go.png" selectedImage:@"button_go_selected.png" target:self selector:@selector(test)];
spritePickMenu = [CCMenu menuWithItems:go, nil];
spritePickMenu.position = ccp(0,0);
spritePick = [CCNode node];
[spritePick addChild:spritePickMenu];
spritePick.position = ccp(240,160);
[self addChild: spritePick];
}
The ‘test’ method, which is called from the button, is simple:
- (void) test {
NSLog(@"%@",spriteType);
}
The NSLog line crashes my game, and gives the error: EXC_BAD_ACCESS
Anywhere apart from inside the ‘test’ method, the code works fine. Why would it be giving me the error when the method has been called from the Node, but it would not give it to me when it has been called from anywhere else?
I can give you the full code if required.
Which attributes are you using for your
spriteTypeproperty declaration?If your project is using ARC, the header should look like:
While the implementation should look like:
If you declare/synthesize your property this way, then
NSLog(@"%@", self.spriteType)just writes (null) to the console (I tested to double-check).It’s a good practice to access your properties using
self.rather than trying to access the backing ivar directly.Since you’re seeing EXC_BAD_ACCESS I assume you aren’t maintaining a strong reference to
self.spriteType.If your project is using ARC, you need to be sure you’re compiling with LLVM 3.0 or greater, as detailed in this answer.