Search code examples
c++cocos2d-x

Cocos2d-x: 0xC0000005: Access violation reading location 0xCDCDCDD1


everyone:

The problem is strange. I paste my code first.

void GameObjectsLayer::updateEnemy(float interval)
{
    UNREFERENCED_PARAMETER(interval);
    Enemy *lpEnemy = new Enemy("enemy1.png", 1, 10, 8);
    lpEnemy->getSprite()->setAnchorPoint(CCPointZero);
        lpEnemy->setPosition(ccp(-30, 400));
    CCMoveTo *lpMoveAction = CCMoveTo::create(350.0f / ENEMY1_MOVE_SPEED, ccp(320, lpEnemy->getPosition().y));
    CCCallFuncND *lpCallback = CCCallFuncND::create(lpEnemy->getSprite(), callfuncND_selector(GameObjectsLayer::removeEnemy), lpEnemy);
    CCSequence *lpSeqActions = CCSequence::create(lpMoveAction, lpCallback, NULL);
    this->addChild(lpEnemy->getSprite(), 10);
    lpEnemy->getSprite()->runAction(lpSeqActions);
    // When I set the breakpoint here, the _vEnemies is valid and the application runs well.
    _vEnemies->push_back(lpEnemy);
}

void GameObjectsLayer::removeEnemy(CCNode *lpSender, void *lpParam)
{
    Enemy *lpEnemy = (Enemy*)lpParam;
    lpEnemy->getSprite()->stopAllActions();
    this->removeChild(lpSender);
    // When access the _vEnemies variable here, the exception occurs at "_vEnemies->begin()"
    // I set a breakpoint here, the address of _vEnemies is invalid, but I set a breakpoint in the previous function, the _vEnemies has a valid address, what happened? I really can't comprehend the behavior. _vEnemies is the member variable of this class.
    std::vector<Enemy*>::iterator iter = _vEnemies->begin();
    while (iter != _vEnemies->end())
    {
        if (lpEnemy == *iter)
        {
            this->removeChild(lpEnemy->getSprite());
            //delete *iter;
            iter = _vEnemies->erase(iter);
            continue;
        }
        ++iter;
    }
    delete lpEnemy;
}

these two methods are used in two CCSchedules, which is intialized in init() method, the codes are below:

bool GameObjectsLayer::init()
{
    bool bRet = false;
    do
    {
        /* not important, omit them */

        this->schedule(schedule_selector(GameObjectsLayer::updateEnemy), 5.0f);
        this->schedule(schedule_selector(GameObjectsLayer::enemyShoot), 1.0f);

        scheduleUpdate();

        bRet = true;
    }
    while (0);

    return bRet;
}

I got a head-ache. I don't know what happened. The version of my cocos2d-x is 2.1.5, and IDE is VS2012. Thanks for help..


Solution

  • Well, this is an old one, but for anyone who might stumble here :

    The problem seems to be in this line

    CCCallFuncND *lpCallback = CCCallFuncND::create(lpEnemy->getSprite(), callfuncND_selector(GameObjectsLayer::removeEnemy), lpEnemy);
    

    The signature for this (from docs) is

    static CCCallFuncND* create ( CCObject *pSelectorTarget, 
                                  SEL_CallFuncND selector,
                                  void * d 
                                )   
    

    *pSelectorTarget is the object on which the selector is called. So in this example it should be this instead of lpEnemy->getSprite().

    Frankly, It seems a little strange to me that it didn't crash earlier.