0

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.

akuritsu
  • 440
  • 1
  • 4
  • 18
  • check the code flow once again, some times we can miss out something.According to my guess "spriteType" is not initialized.some where in your code write following line and then check self.spriteType=@""; – ajay May 31 '12 at 10:03
  • Perhaps you are doing something wrong with memory, creating a zombie, that ends up crashing the app. Providing the full code looks like a good idea. – Lio May 31 '12 at 12:53

1 Answers1

1

Which attributes are you using for your spriteType property declaration?

If your project is using ARC, the header should look like:

@property (nonatomic, strong) NSString *spriteType;

While the implementation should look like:

@synthesize spriteType = spriteType_;

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.

Community
  • 1
  • 1
Joshua Smith
  • 944
  • 6
  • 10
  • When I use this, I get an error on the @property: "Expected a property attribute before 'strong'" – akuritsu Jun 01 '12 at 06:28
  • Is your project using ARC and compiling with GCC? You need to compile with LLVM/Clang. Updated my answer accordingly. – Joshua Smith Jun 01 '12 at 13:00
  • At the moment I am not using ARC, as I am using a cocos2d template, and therefore it was not automatically enabled. I can enable it though, but is it worth it? – akuritsu Jun 02 '12 at 02:18
  • If you aren't using ARC, try changing (nonatomic, strong) to (nonatomic, retain), leaving the rest I mentioned above. – Joshua Smith Jun 02 '12 at 02:45