How to release space used by CCSprites

I have created a number of CCSprites using CCSprite::spriteWithFile(“”);
and when i’m switching the scene using replacescene
memory acquired during last scene is not released.
How can I release that space?

Have you retained the object?

Hi, Neel, I thinks you had to call CCTextureCache::purgeXXXX series function to remove useless textures from memory.

I’m a newbie with cocos2d-x, as well, and I also would like to know about releasing the memory (if needed).

The simple examples I’ve seen show how to load a sprite via CCCprite::spriteWithFile(“”), but as far as I can tell,
when you switch scenes via replaceScene(), none of the code explicitly calls a function to release
the memory. release() is called only on the destructor for the scene/layer which the sprite as added to.

So, if you’re switching among different scenes via replaceScene() (e.g., in a game where each level is a different
scene/layer) and the memory used to create each scene is not freed, then I can see that the device would
run out of memory really quick (unless I’m missing something obvious and that replaceScene() will call
the destructor for all objects in the scene/layer being replaced??)

I don’t know how others are doing it in their own game/app (I definitely would like to see some
examples), but it’s not clear/obvious how to do proper memory management without looking at what the
actual cocos2d-x source is doing (guess I’m used to explicitly calling malloc()/free() and knowing what objects
are created on the heap that need to be freed later).

u can try removeAllChildrenWithCleanup(true) in onExit() of your scene,
this can release the objects that you have added child of your scene,
but for the retained object u should release it manually.

retain and release “steal” from objective-c. They are used for sharing object.
It use referent count to manage the memory. May be you can refer the doc of apple
http://developer.apple.com/library/ios/#DOCUMENTATION/General/Conceptual/DevPedia-CocoaCore/MemoryManagement.html

So, if you're switching among different scenes via replaceScene() (e.g., in a game where each level is a different
scene/layer) and the memory used to create each scene is not freed, then I can see that the device would
run out of memory really quick (unless I'm missing something obvious and that replaceScene() will call
the destructor for all objects in the scene/layer being replaced?!?!?)

I just realized that I’m switching between scenes and my objects and memory is not freed and the deconstructors are not being run on the scene. Is this normal? When I replacescene how can I free up memory in the old scene? If the scene is autorelease does it happen automatically? And if it’s retained, when can I manually release the scene…before or after replacing the scene? I seem to get errors.

It seems that you misused retain and release. The reference of CCSprite is greater than 0, so it will not be release.

Chad, I’m in the same situation. From the cocos2d-x examples I’ve seen, I haven’t found one that talks about scene management, since most of them are just simple, single-scene examples. And if they do switch scenes, they just call replaceScene() and do not show any explicit calls to retain or release. I bet if I looked into the source for some of these cocos2d-x APIs, then maybe they do call retain, meaning that we would have to call release. But it’s not obvious from a cocos2d-x beginner that you have to call release for a cocos2d-x function, unless you look at the source of all these functions before you use them. I guess if there were more documentation on the APIs themselves that actually say that a function internally calls retain, then us beginners would know that we have to call release afterwards.

In my quest to learn, my example programs so far do not explicitly call retain or release. I just follow a simple example, create a couple of simple scenes, and just call the cocos2d-x functions. I have no idea from the cocos2d-x functions that I have to call some release function after making a call to that cocos2d-x function. Maybe I’m a slow learner, but it’s really not obvious.

The memory management is the same as object-c.
I think you should refer http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/MemoryMgmt/Articles/mmRules.html#//apple_ref/doc/uid/20000994-SW1.

Im using release and purgeTexture and see that textures must be load again but memory was not free. Im using CCAnimations so maybe im doing something wrong, but I already release() and removeWithClean as many things as I could see and dont free any memory! :confused:

If you are using CCAnimation you should try to purge animation cache too.

Hi Igor,

How do you purge the animation cache?

This post was very informative for me but i have one question

Lets say I create a sprite with CCSprites using = CCSprite::spriteWithFile(“”)
I call retain method on it using.retain
add it to the parent with this.add
then I call using.release before I remove the sprite from parent

will this lead to a crash since I passed a reference to parent?