How to safely use CCCallFuncN in CCSequence?

How to safely use CCCallFuncN in CCSequence?
0.0 0


I’ve noticed occasional crashes from writing code such as this:

CCFiniteTimeAction *seq = CCSequence::actions(
CCMoveBy::actionWithDuration(0.5, ccp(0,30)),
CCCallFuncN::actionWithTarget(this, callfuncN_selector(ContainerClass::removeBonusPoints)),


The idea here is that a label would float up and fade out and then be removed. The problem lies in that if the object is deleted before the sequence is through, the CCCallFuncN will cause a crash.
What is the proper way to run an animation sequence and have the node be removed from the stage upon completion? Or even just a safer way to say call this function IF the object still exists?


I think your code should work fine, I did similar things and it worked without a problem.
If the CCCallFuncN is the one crashing, maybe you can call retain on it, so the reference count won’t be zero, and later release it.


I sometimes get crashes like this:
In your experience, if you retain before calling the sequence and release or autorelease in the callback, should this be avoided?

Thread 0 name: Dispatch queue:
Thread 0 Crashed:
0x0029ee22 cocos2d::CCCallFunc::CCCallFunc\
\ 0x0029f262\ cocos2d::CCCallFuncN::
CCCallFuncN() (CCActionInstant.h:259)
0x0029f156 cocos2d::CCCallFuncN::CCCallFuncN\
\ 0x0029f1da\ cocos2d::CCCallFuncN::
CCCallFuncN() (CCActionInstant.h:259)
0x002c0c2a cocos2d::CCObject::release() (CCObject.cpp:72)
0x0029fccc cocos2d::CCSequence::CCSequence\
\ 0x0029fbce\ cocos2d::CCSequence::
CCSequence() (CCActionInterval.cpp:241)
0x0029fb46 cocos2d::CCSequence::~CCSequence() (CCActionInterval.cpp:241)
0x002c0c2a cocos2d::CCObject::release() (CCObject.cpp:72)
0x002ba768 cocos2d::CCMutableArraycocos2d::CCObject*::removeAllObjects(bool) (CCMutableArray.h:252)
0x002ba068 cocos2d::CCAutoreleasePool::clear() (CCAutoreleasePool.cpp:78)
0x002ba418 cocos2d::CCPoolManager::pop() (CCAutoreleasePool.cpp:147)
0x002b0ec8 cocos2d::CCDisplayLinkDirector::mainLoop() (CCDirector.cpp:938)


I think the problem may be “this” is deleted when CCCallFuncN use it to call back ContainerClass::removeBonusPoints().
So you can check it.



I have a similar problem. In mi case I have a class which manages a CCSprite where the animation is executed. And the callback method is defined in the class “ContainerClass” following the example above.

The problem is that when I destroy ContainerClass I call stopAllActions() for the CCSprite. But the error keep appearing. Stoping all the actions is not enought.

What can I do? I need to destroy this object (ContainerClass) before the CCSprite which is managed by a superclass. But in seconds the CCSprite will be destroyed by the superclass destroy method.

Thank you in advance!