[SOLVED] "Unhandled exception" when trying to access a CCActionInterval from another function... (C++)

[SOLVED] "Unhandled exception" when trying to access a CCActionInterval from another function... (C++)
0.0 0


If you plan on storing a CCAction in any of its forms into a variable :**Always retain the variable* or its data will be scrambled!

    CCFiniteTimeAction* act = CCMoveTo::actionWithDuration( 1, ccp(250,250) );

Thanks to Walzer Wang and Minggo Zhang for their help!

Hi guys, I’m trying to keep track of the status of the animations on some sprites. I’ve narrowed down my code so it’s easy to see where the problem is born from.
The main problem is: I want to move a sprite, and turn its color into purple when it finishes the CCMove action. What I do is define the
CCActionInterval* (also tried with CCFiniteTimeAction) as a member of the class, define its type on the onEnter function, and try to know it’s status on the update function. Here’s the code:


class QuizLayer : public CCLayer{
    CCSprite* sprite; // The sprite
    CCActionInterval* act;
    void onEnter();
    void update(ccTime dt);

    static QuizLayer* node(){
        QuizLayer* pNode = new QuizLayer();
        return pNode;


void QuizLayer::onEnter(){
    sprite = CCSprite::spriteWithFile("hello.png");
    ccs->setPosition( ccp(100,100) );

    act = CCMoveTo::actionWithDuration( 2, ccp(200,200) ); // Definition of the actionInterval
    this->schedule( schedule_selector(QuizLayer::update) ); // Add the update function to the scheduler

void QuizLayer::update(ccTime dt){
    // OPTION 1 - Call "act" directly
    if (act->isDone()){
        sprite->setColor(cPurple); // ccColor3B cPurple = {190,0,214}; // Turn sprite color into purple
    // OPTION 2 - Get the action by its TAG in the sprite
    if (sprite->getActionByTag(111)->isDone()){


When arriving at any of the IFs on the update function, VS2010 prompts me the following:

( Unhandled exception at 0x010d55e9 in TSP.win32.exe: 0xC0000005: Access violation reading location 0x00000000.

I don’t know if this is even achievable, but if someone can lend me a hand, I’ll appreciate it very much)
Thanks in advance!


The problem is probably caused by the autorelease pool. The mechanic is described here http://www.cocos2d-x.org/boards/6/topics/678?r=696
In your node() function

 static QuizLayer* node(){
        QuizLayer* pNode = new QuizLayer();   // QuizLayer::m_uRefCount = 1
        pNode->autorelease();    // will be release at the end of this msg loop
        return pNode;

If you haven’t call MyScene->addChild( QuizLayer::node() ), or QuizLayer::node()->retain() in you scene, QuizLayer object will be released before update() call in next message loop.
It’s only my conjecture. :slight_smile:


Thanks for the reply Walzer! :smiley: Though I’m afraid I’m still stuck on this…:frowning:
I can’t say that I understand completely the behaviours of autorelease(), retain(), and so on, I’m kinda new to all of this methods :frowning: but from what I’ve seen I think my problem resides on the main scene.
I dumbed down my code so that it’s (maybe) easier to read… The main scene is taken from the layer Test now.

CCScene *pScene = GameStarter::scene();

GameStarter:public CCLayer

class GameStarter : public CCLayer{
    static cocos2d::CCScene* scene(){ // AppDelegate calls this
        return Test::scene();

Test:public CCLayerHEADER

class Test : public CCLayer{
    CCSprite* ccs; // Test sprite
    CCFiniteTimeAction* act;

    static Test* node();
    static CCScene* scene(); // What is requested by GameStarter and AppDelegate

    void onEnter();
    void update(ccTime dt);

Test:public CCLayerCPP

Test* Test::node(){
    Test* pNode = new Test();
    return pNode;

CCScene* Test::scene(){
    CCScene * scene = NULL;
    do {
        // 'scene' is an autorelease object
        scene = CCScene::node();
        CC_BREAK_IF(! scene);

        // 'layer' is an autorelease object
        Test *layer = Test::node();
        CC_BREAK_IF(! layer);

        // add layer as a child to scene

    } while (0);

    // return the scene
    return scene;

void Test::onEnter(){

    ccs = CCSprite::spriteWithFile("HELLO.PNG");
    ccs->setPosition( ccp(100, 100) );

    act = CCMoveTo::actionWithDuration( 1, ccp(250,250) );
    this->schedule( schedule_selector(Test::update) ); // Add the update function to scheduler

void Test::update(ccTime dt){
    if (act->isDone()){
        sprite->setColor(cPurple); // ccColor3B cPurple = {190,0,214}; // Turn sprite color into purple

So simple, and it still breaks down on the same thing! I don’t know what to do…
I’m going to conitue try to find this issue and learn more about autorelease() and his “friends”, but if anyone notices something out of place, please drop a line!
Thanks in advance.


I think there are two errors:

  1. onEnter() may be called many times, so don’t init “ccs” and “act” in it. I think call init() in constructor is a suitable method.
  2. You should retain “css” and “act” after you invoke CCSprite::spriteWithFile() and CCMoveTo::actionWithDuration(), or it will be released, because CCSprite::spriteWithFile() and CCMoveTo::actionWithDuration() all call autorelease().


OMG that’s what it was:

When I retained the action, it automatically worked (/)

Thank you SO MUCH Minggo, I’ll update my first post for other people to see :smiley: