Does memory released after I removed sprites?

I am complete beginner.
I want to ask If we replace scene or remove sprites, does the memory release automatically?

if you create scene or sprites by ::create() method, then YES.

It means after I create sprite and destroy them I don’t have to worry about memory. and I can create many sprites as I want provided I am destroying them after there usage.
Sorry for my english

No, you don’t have to worry about it. :slight_smile:

yes they will automatically released/destroyed when the release count is down to 0 but you should be careful when you are using retain() function because that will increment the release count on any objects that derived from cocos2d::Ref class. So you have to make sure to use release() function for every retain() function called earlier

1 Like

sprites are autoreleased objects
so, when reference count is 0 , they will be removed
as when they are created they are added to the autorelease pool.
and autorelease keeps the object count, when the object count is zero, it releases the object.

1 Like

Thank you,
Can you please tell me the right method to destroy sprites after performing a specific action?

when you create sprite by auto sp = Sprite::create(),
sp reference count is 1.

If you don’t addChild(sp) until next frame,
sp reference count is set to 0 and will be deleted.

If you do addChild(sp) before next frame,
sp reference count is 1 on next frame and it remains on memory.

If you do removeChild(sp) at some point,
sp reference count is 0 and will be deleted on memory.

3 Likes

as @hzlov said that’s the right action, but if you don’t know the parent object. you could use removeFromParent() from the sprite object.

1 Like

Thanks for this beautiful reply
But I want to run some moveto or some actions on sprite and want sprite to be deleted or destroyed after performing this action automatically.

well, you could use Sequence to run list of actions sequentially and then at the end of the sequence use CallFunc or CallFuncN action to call a function that will release/remove/delete that sprite. For more information about those actions, you could read the example on cpp-test

There’s also the specific action RemoveSelf

Sequence::create(MoveTo::create(0.5, Vec2(10,10)), ..., RemoveSelf::create(), nullptr);
2 Likes

But removing the sprite does not release the cached texture, correct? One still has to do that manually once you are sure you are done with a texture.

I do it when I transition to a new scene. For example in MyGameScene.m:

void MyGame::onEnterTransitionDidFinish()
{
    Layer::onEnterTransitionDidFinish();
    Director::getInstance()->purgeCachedData();
}

But this also assumes you are not preloading any textures that are not currently being used.

Good point. Though if you remove all textures between levels or scene transitions you’ll get larger loading times. I would preload all textures until you get to a point where your texture resource usage is too high for your low-end devices that you want to support.

Also, one needs to remove any SpriteFrames that use that texture from the frame cache as well otherwise the texture will be kept around as it’s still retained by the frames. Then remove the textures from the cache.

For our games we remove from cache immediately to keep it in the same location as where it’s loaded.

/* load image as texture, or by creating sprite */
Director::getInstance()->getTextureCache()->removeTextureForKey(filepath);
1 Like

Thanks for making it really clear