How to release memory when scene replace in cocosd2d-x 3.2

I am facing serious memory problem , when i play my game continuously then my game memory increases & not release when scene replace … so gradually it gives me memory warnings and then it start crashing on ios devices . can some one please help me on this !? I am using cocos2d-x 3.2 …

how are you initiating sprites? and when you make a new class obj how do you initiate it?

I create sprite using create() method … when i create new class obj i use new keyword like this :
MyClass *MyClassObject = new MyClass();

can you help how to release memory !!..

MyClass *MyClassObject = new MyClass(); // if this is derived from cocos Node or layer
//always do this

MyClass *MyClassObject = new MyClass();
MyClassObject->autorelease();

okey , understood & except that all resources memory would free automatically on scene replace ??!! . . . .

If you allocate memory with new then somewhere later it had to be de-allocated. The autorelease will not be able to release memories it doesn’t know about. If inside your MyClass you created objects then you’ll have to delete them yourself unless you use C++ smart pointers.

  • If you allocate memory with new then somewhere later it had to be de-allocated.
    -> i understand this . i have to autorelease object .

  • If inside your MyClass you created objects then you’ll have to delete them yourself unless you use C++ smart pointers.
    -> can you please explain this more briefly , you mean to say if i have again created objects using new class in my MyClass i have to autorelease them !!! .

please help me on this , my game memory increases as game progresses & it kills my game …thanks

If you are running the game on iOS then you could try profiling it using Instruments. That might pin-point the origin of the leak(s).

thanks … yaa i am profiling too simultaneously & i can see there is some minor leaks too . but i feel that could be not this major reason … memory allocation graph is simply exponential & so i feel the memory is not releasing at all …

  1. See this Memory Management, they can explain it better than me
  2. C++ Smart Pointers

autorelease is the mechanism provided by Cocos2d-x. For example if your class inherits from Ref then it can participate in the autorelease. But any objects created inside that class, which itself isn’t a descendant of Ref, then you’ll have to delete that manually when you don’t need it any more.

Thanks for your help. let me try on this.

Are you using pushScene() etc? I recently had the same issue and it was related to attempting to getting back to the main menu scene from the pause menu of my game.

No , i am not pushing or popping scenes , i have just use replace scene .

i got one continuous memory leaks relate to my sqilte database saying that sqlite3Memalloc … i don’t know that it might causing memory issues…

Have you tried autorelease? i had the exact same issue as you and it worked for me…

Another way is to put all objects in an array and before replacing scene do something like this

for (auto obj : array)
{
obj->removeFromParentAndCleanup(true);
obj = null;
}

i have already used autorelease at all necessary places & your idea of releasing objects seems nice but is cocos-x not providing any mechanism of freeing all resources while replacing scene ? !! that makes me curious … still thanks for your helps…

Well if your error mentions sqlite3, then this has nothing to do with Cocos2d-x at all.

Its probably not because of cocos. Cocos actually handles memory management pretty well.

1 Like

yaa ,thats correct . . but resources not getting release its also there :slight_smile:

Have you tried removing unused textures after you replace scene?