Search code examples
objective-cmemory-managementcocos2d-iphone

Can't understand what is wrong, get EXC_BAD_ACCESS, any hints?


I'm having this sprite with this in it's init method

lineDrawer = [[LineDrawer alloc]initWithLineColor:ccc3(color.r, color.g, color.b)];
lineDrawer.position = ccp(0,0);
lineDrawer.anchorPoint = ccp(1,1);

[self addChild:lineDrawer];
[self addChild:head];

[lineDrawer release];

Nothing wrong here right? I can release it as addchild is retaining the object. (autorelease would also be ok)

Although when I have the release I get EXC_BAD_ACCESS(code=1) later on. I've also tried to move the release to the dealloc method but it gives me the same error.

The only object that has reference to lineDrawer is the spriteobject (Player : CCSprite) it's defined in and It doesn't have a getter so it cant even be accessed by another object, so I don't see how this can happen. What can be calling lineDrawer at the time It's dealloced when only the owner has a reference to it?

It happens on the line CC_NODE_DRAW_SETUP() inside the draw method of CCLayer.

Any hints? I know these questions is always hard to solve with the lack of code but It's really hard to know what code to show you. If I knew I could probably solve it by myself. I've also tried to debug this some in instruments but I'm not sure where to look.

Update, I tried debugging with zombies enabled

This is the result

Does this say anything to you? This is the LineDrawer class. It's someone else's class that I've slightly modified.

http://pastebin.com/7Uv4GULy
http://pastebin.com/08NHeVt9

I have a feeling that its some method that's scheduled or something..


Solution

  • Since you add the lineDrawer object to the child collection of your CCSprite, a public reference to it is available through the .children property defined in CCNode.

    However, nothing in the code you've posted seems wrong, except that LineDrawer inherits from CCLayer and I don't think that CCLayer was ever designed to be used as a child of a CCSprite ... does it really need to be a child of your Player object? (e.g. should it be following your Player sprite as it moves and rotates?) Typically a layer is a child of your active CCScene node, or another layer.