May anyone help me with this bug?

this is my code

    Zombie::Zombie() : 
     _Acttack(nullptr)
    , _runAction1(nullptr)
    { }
    
    
    Zombie::~Zombie()
    {
    	if (_runAction1)
    	{
    		_runAction1->stopAllActions();
    		_spriteBatchNode->removeChild(_runAction1, true);
    	}
    	CC_SAFE_RELEASE(_Acttack);
    }
    Zombie * Zombie::createZombie(SpriteBatchNode* gameBatchNode) {
	auto zombie = new Zombie();
	if (zombie) {
		zombie->setSpriteBatchNode(gameBatchNode);
		zombie->init();
		zombie->autorelease();
		zombie->setIsZombie(true);
		return zombie;
	}
	CC_SAFE_DELETE(zombie);
	return nullptr;
}
    void Zombie::init() {
      .............
    		_Acttack = Sequence::create(ActionGame::_zombieActtack->clone(),
    			CallFuncN::create(CC_CALLBACK_1(Zombie::animationDone, this)),
    			nullptr);
    		_Acttack->retain();
    		_runAction1 = GameSprite::gameSpriteWithFrame("blankspace.png");
    		_runAction1->setVisible(false);
    		_spriteBatchNode->addChild(_runAction1, kForeground);
        ................
    	}
    }

when game played everything is good (i can destroy zombie by release it), but sometime when i exit GameLayer to MenuLayer (all of zombie will be destroy) i have some bug because two line
_runAction1->stopAllActions();
or
_spriteBatchNode->removeChild(_runAction1, true);

my english is not good, sory about that.

  1. Try printing/debugging the ref count of those two nodes. Make sure that they as you expected, i.e., retain & release are called correctly.

  2. Check that _spriteBatchNode isn’t nullptr before that call

getReferenceCount

1 Like