Monday, August 29, 2022
HomeGame DevelopmentHow one can keep away from a crash when when a node...

How one can keep away from a crash when when a node is deleted in replace loop? (see instance for clarification) – cocos2d-x


Hello,
let me wright this pseudo code first after which clarify. Say GameScene is a recreation stage. GameScene has a personal Sprite member known as _mySprite declared within the header as nullPtr. Within the replace methodology I’m attempting to print one thing if the sprite exist, after which delete that sprite.

bool GameScene::init()
{
    //... code
    _mySprite = Sprite::create("someTexture.png") ; 
    _mySprite->setPosition(500, 500);
    addChild(_mySprite);
    //... code
}

void GameScene::replace(float dt)
{
	if (_mySprite)
	{
		CCLOG("test level A");
		_mySprite->removeFromParentAndCleanup(true);
	}
}

And that is the error

test level A
test level A
Exception thrown: learn entry violation.
this->_testSprite->**** was 0xDDDDDDDD.

My understanding is that 0xDDDDDDDD signifies that the pointer is null since I eliminated it with removeFromParentAndCleanup(true), proper? But when I set the situation to enter the scope provided that _testSprite exist (if (_mySprite)), why is it coming into once more after take away it?

My answer is to create a vector holding that sprite, and if I take away it, I delete it from the vector and the insteado of (if (_mySprite)), I do one thing like (if (myVector.measurement() > 0)). Or possibly utilizing tags, however I’m wondering why my try on checking if a pointer isn’t null shouldn’t be working. How would you strategy one thing like that?

Thanks


That removes it from its guardian, but it surely doesn’t clear the _mySprite variable. No matter that variable is pointing to is now not legitimate, which is what this implies:

If it was null, then it could throw a special error associated to de-referencing a null pointer. That can be why if (_mySprite) shouldn’t be catching it, as a result of it’s by no means null.

Try to be doing this:

_mySprite->removeFromParentAndCleanup(true);
_mySprite = nullptr;

You must really be calling retain explicitly on any Ref occasion (equivalent to a Sprite) when you’re holding it in a storage sort that doesn’t monitor the ref rely. For instance, when you maintain it in a category member variable, or in a std::vector, there is no such thing as a manner to make certain the pointers they’re holding are legitimate. That’s the reason you’d explicitly name retain on creation, after which launch whenever you’re carried out with the occasion. Alternatively, you need to use the cocos containers, equivalent to cocos2d::Vector, which mechanically name retain and launch, guaranteeing that the cases they level to are at all times legitimate so long as they’re within the container.

So, for instance, your code needs to be like this:

_mySprite = Sprite::create("someTexture.png") ;
_mySprite->retain();
_mySprite->setPosition(500, 500);
addChild(_mySprite);

Then afterward whenever you’re carried out with it:

_mySprite->removeFromParentAndCleanup(true);
_mySprite->launch();
_mySprite = nullptr;

// OR you need to use the CC_SAFE_RELEASE_NULL macro, which does the identical factor:
_mySprite->removeFromParentAndCleanup(true);
CC_SAFE_RELEASE_NULL(_mySprite);

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

- Advertisment -
Google search engine

Most Popular

Recent Comments