CCMenu is causing an EXC_BAD_ACCESS error, but I can't backtrace my code

96 Views Asked by At

I'm having EXC_BAD_ACCESS problems with cocos2d-iphone 1.0.1.

I have enabled NSZombies, hoping to see where in my code am I accessing an object I shouldn't be accessing.

The problem occurs in CCMenu.m, here:

-(void) ccTouchMoved:(UITouch *)touch withEvent:(UIEvent *)event
{
    NSAssert(state_ == kCCMenuStateTrackingTouch, @"[Menu ccTouchMoved] -- invalid state");

    CCMenuItem *currentItem = [self itemForTouch:touch];

    if (currentItem != selectedItem_) {
        [selectedItem_ unselected];   // <--- selectedItem_ is a zombie
        selectedItem_ = currentItem;
        [selectedItem_ selected];
    }
}

I can observe that selectedItem_ is a zombie.

This is CCMenu code, not mine - I should be looking at my own code. So I take a look at the backtrace:

enter image description here

This doesn't really help me: The backtrace does not reach my own code. That's cocos2d-iphone code.

What should I do to address this error then?

1

There are 1 best solutions below

4
Mr Bonjour On

Omega, you should be careful with ownership:

if (currentItem != selectedItem_)
        selectedItem_ = currentItem; // is plain wrong on non ARC mode.

You should synthesize retained setter for selectedItem_

@property(nonatomic, retain) CCMenuItem* selectedItem;

...

@synthesize selectedItem;

Then using setter assignement. Not plain pointer assignment.

self.selectedItem_ = currentItem;
// is actually kind of
// [selectedItem_ autorelease]
// selectedItem_ = [currentItem retain]