Memory Issue in cocos2dx - c++

Thanx in advance..I have a problem , how we release memory in cocos2dx ??
In my game, my first scene takes a lot of memory because there are so many animations run at a single time on this scene,there are so many animations , so i am satisfied with this but when we go to next scene, it does not release previous memory used,this is my problem so how we release memory used by previous scene when we change the scene?

To go between scenes you can either call pushScene or replaceScene.
Push Scene
If you're pushing between scenes then I recommend that you have a loading scene in between. It should be lightweight so that you have a chance to release everything from your old scene. This is where the onEnter(DidFinish) and onExit(DidStart) methods come in handy.
The chain of calls would be:
oldScene->onExitDidStart()
loadScene->onEnter()
oldScene->onExit() <-- this is where you release everything
loadScene->onEnterDidFinish() <-- this is where you load up the new scene
newScene->onEnter()
and so on... If you've managed your memory correctly then you shouldn't have 2 heavy scenes at once.
Replace Scene
This is a much easier scenario. You simply need to make sure that for the new scene you load as little as possible until the old scene has completely disappeared. I.e. when onEnterDidFinish() is called, or even 1 frame after it.

Cocos2d-x supports auto release memory. For example, when you create a sprite by calling Sprite::create() it will add this sprite to the auto-release pool. If you don't add this sprite to become the child (or n-th leveled child) of the current scene, it will be released.
Do not create an object using new,use create() instead when you call the next scene, everything from the last scene are released.

Related

Take few seconds to load the scene cocos 2d

I have used
[[CCDirector sharedDirector] replaceScene:[CCBReader sceneWithNodeGraphFromFile:#"SongScene.ccbi"] ];
For load the next scene. It take a little delay to load the scene. When the second time loading, there is no delay. How can i fix the issue. Songs scene consist of many graphic sprites.
When you first start your game, create an Intro Scene showing a splash image or loading image, etc. While that scene is showing, load up the resources that will take a long time to load so that they are already in the cache.
When they are done loading, transition to your main scene(s). The resources will already be loaded and they will go faster.
You can even create entire scenes like this, you just need a place to store them off temporarily, such as the child of a CCNode that you use for holding scenes. Then use them when you need them.
I only learned this recently. Before that I was loading them at the start of the App, which is a poor choice.

Does replaceScene replace only the current scene or does it replace all scenes in Cocos2d

I have a game in Cocos2d with a main scene (game scene) and a button to go to a "Configuration" scene. When the user clicks on the Configuration button in the main scene I use pushScene to go tho the "Configuration" scene. The reason I use pushScene is to allow the user to resume the game where he was left off.
In the "Configuration" scene there are two options: "Cancel" and "Ok". If the user hits "Cancel" I use popScene and the game resumes where it was left of. If the user hits "Ok" I use replaceScene because I want the game to start from the beginning with the new configuration.
So, when the user hits "Ok" I know that the "Configuration" scene is replaced by the new game scene, but does the old game scene gets replaced too? Otherwise, am I doing things correctly or should I implement another way to let the game scene know whether it should resume or restart.
I want to make sure I am not leaking memory by accumulating unreplaced scenes.
The replaceScene method does what it says. It replaces the current scene. If you have 10 scenes pushed onto one another, it will replace the 10th scene and all previous scenes remain.
It's one of the reasons why I don't recommend using pushScene. It's too easy to forget a situation where scenes might get pushed more than they get popped. The other reason is that popScene can't be animated with a transition.
Btw, you can easily test this behavior if you do replaceScene after pushScene, then popScene in the newly replaced scene. You'll see the old scene popping up. Normally if you popScene with just a single scene in the stack it'll throw an assertion.

cocos2d-iphone Fade-Out Sprites Need Cleanup?

I'm developing an iPhone game using cocos2d.
As the player moves around and scores points by doing things, I cause little CCLabelBMFont instances to appear and then fade out (CCFadeOut). These CCLabelBMFont instances are sprites that are added to the layer.
Am I "leaking" memory or anything by not removing the faded-out sprites from the layer after they have completed the CCFadeOut action, or are they then gone, or don't need to be considered "valid"?
After CCFadeOut has finished, the layer will keep hold of the now transparent label. You'll have to remove it from the layer manually afterwards, unless you are planning to fade it back in later, in which case you could keep it around.
It is inefficient memory usage, not a memory leak.
You aren't causing a memory leak because when the layer is deallocated it will deallocate its child nodes, including the labels, assuming you are not instantiating the labels in a way that doesn't trigger a retain.
Update: how to remove the labels after they fade out
Replace your CCFadeOut with a CCSequence that looks like this:
[yourLabel runAction:[CCSequence actions:[CCFadeOut actionWithDuration:DURATION], [CCCallFuncN actionWithTarget:self selector:#selector(removeLabel:)], nil]];
Now implement your new handler removeLabel: and make it remove the label. It takes the label as its argument.
CCFadeOut does NOT remove nor releases your sprites and you should do it once their purpose is over.
Infact no action does release/remove other objects. They are only manipulating them.

whats happen when leaving a CCscene in cocos2d?

using cocos2d.
i need to know what happen when leaving a scene(replace scene) with the next:
shared frame cache (plist and png 's that where spriteSheets )
sprites (the removed?)
actions (they stoped and released?)
bodies (box2d) (i have to destroy them or its auto? )
is that happen auto , or i need to do something on the dealloc( ex: removing childs)
thanks .
All caches remain unchanged, it's up to you to remove textures, sprite frames, etc. as needed.
Sprites and all other nodes of the scene are removed automatically. So are actions.
Box2D is a separate library and not integrated in cocos2d's source code. The Box2D source code is merely distributed with cocos2d-iphone. That means you are responsible for cleaning up all Box2D objects and the world. There's nothing automatic there.

Using replace scene to navigate

I have a question with regards to using replace scene in cocos2d.
Assume my game menu has the following structure:
Main Menu
Play
2.1 Strategy levels
2.2 Accuracy levels
Settings
Tutorial
Is the recommended method of navigation between the menus, "replace scene" or "push/pop"?
I've read in some places that its good to avoid push/pop in cocos2d. But my concern in using replace scene is that i have the impression that im just stacking up scenes instead of having a proper navigational flow.
Push and pop scene is going to stack your scenes. Each time you push a new scene, the old scene remains in memory and you need the exact same amount of popScene to get back to the initial scene.
So yes, using replaceScene is the standard and recommended method of switching scenes. While replaceScene removes the original scene from memory, there is a short time of overlap where both scenes remain in memory. So if you have two very memory intensive scenes, it is recommended to go through a temporary loading scene instead to allow the previous scene enough time to release its memory.
Note also that you cannot call replaceScene within the init method of a scene. That will cause CCDirector to crash.