[ad_1]
Hi,
let me wright this pseudo code first after which clarify. Say GameScene is a recreation degree. GameScene has a non-public Sprite member known as _mySprite declared within the header as nullPtr. In the replace technique 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("verify level A");
_mySprite->removeFromParentAndCleanup(true);
}
}
And that is the error
verify level A
verify level A
Exception thrown: learn entry violation.
this->_testSprite->**** was 0xDDDDDDDD.
My understanding is that 0xDDDDDDDD implies that the pointer is null since I eliminated it with removeFromParentAndCleanup(true), proper? But if 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 resolution 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.dimension() > 0)). Or possibly utilizing tags, however I’m wondering why my try on checking if a pointer isn’t null just isn’t working. How would you strategy one thing like that?
Thanks
That removes it from its dad or mum, nevertheless it doesn’t clear the _mySprite
variable. Whatever that variable is pointing to is not legitimate, which is what this implies:
If it was null, then it might throw a special error associated to de-referencing a null pointer. That can be why if (_mySprite)
just isn’t catching it, as a result of it’s by no means null.
You needs to be doing this:
_mySprite->removeFromParentAndCleanup(true);
_mySprite = nullptr;
You ought to really be calling retain
explicitly on any Ref
occasion (comparable to a Sprite
) in case you’re holding it in a storage sort that doesn’t monitor the ref depend. For instance, in case you maintain it in a category member variable, or in a std::vector
, there isn’t a method to make certain the pointers they’re holding are legitimate. That is why you’ll explicitly name retain
on creation, after which launch
while you’re completed with the occasion. Alternatively, you should utilize the cocos containers, comparable to cocos2d::Vector
, which routinely name retain
and launch
, guaranteeing that the situations they level to are all the time 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 while you’re completed with it:
_mySprite->removeFromParentAndCleanup(true);
_mySprite->launch();
_mySprite = nullptr;
// OR you should utilize the CC_SAFE_RELEASE_NULL macro, which does the identical factor:
_mySprite->removeFromParentAndCleanup(true);
CC_SAFE_RELEASE_NULL(_mySprite);
[ad_2]