I'm confused about something. I've got a CocosNode <TargetedTouchDelegate> that works fine if I leave the scene it is used in by loading another scene, but if I quit the app by pressing the exit button, it throws an error saying that I'm doing a "double free".
I've narrowed it down to this:
@implementation TestButton
+(id)node {
return [[[TestButton alloc] init] autorelease];
}
-(id)init {
self = [super init];
if (self != nil) {
[[TouchDispatcher sharedDispatcher] addTargetedDelegate:self priority:0 swallowsTouches:YES];
}
return self;
}
- (void)dealloc {
[[TouchDispatcher sharedDispatcher] removeDelegate:self];
[super dealloc];
}
@end
The removeDelegate line is where the problem happens. It removes the delegate fine if dealloc is called as a result of me replacing the scene. If it's called due to an applicationWIllTerminate, then I get the double free message. I think what's going on is that the touch dispatcher is purging its list of delegates before I get my dealloc message, so when my code gets called, it's already freed, or something.
Obviously, I'd like to correct this problem in my code, but the larger issue is that this is leading me to believe I am using the touch dispatcher in the wrong way. What's the proper way to register and remove touch dispatcher delegates? Am I doing something wrong, or is this a bug?