I'm trying to make a callback layer that will execute a function when it is about to be removed. The problem is that the retainCount on the CCCallFunc never reaches zero.
The pause menu is using this in the code below (very bottom). Every time the pause menu is created, the retainCount for the parent layer's CCCallFunc increases by one extra. Because of this, the parent layer never can be dealloc'ed.
The extra increase comes from [CCCallFunc actionWithTarget:self selector:@selector(pauseDone)].
Digging deeper, the _callback retainCount never reaches zero. When the extra release is added in, everything seems to work with no problems but this shouldn't be needed.
@interface CCCallbackLayer : CCLayer {
CCCallFunc* _callback;
}
-(void) enableCallback:(CCCallFunc*) func;
@end
@implementation CCCallbackLayer
-(id) init
{
if( (self=[super init]))
{
_callback = nil;
}
return self;
}
-(void) enableCallback:(CCCallFunc*) func
{
_callback = func;
[_callback retain];
}
-(void) dealloc
{
// Trigger the callback before we get deleted
if (_callback) { // _callback retainCount=1
[self runAction: _callback]; //_callback retainCount=2
[_callback release]; // _callback retainCount=1
}
[super dealloc];
}
@end
------------------
// Open up the new pause menu which is derived from CCCallbackLayer
//
-(void) pauseTapped:(id)sender {
PauseLayer* pause = [PauseLayer node];
[pause enableCallback:[CCCallFunc actionWithTarget:self selector:@selector(pauseDone)]]; // CCCallFunc retainCount increases
[[self parent] addChild: pause];
}