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

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?

ya i am removing Unused textures using this : texturecache->removeunusedtextures .

If i am deleting every objects on replace scene then it seems working . no more memory consumes but i have to delete each n every objects of my scene manually … just like u mentioned. is this right way to do it ?!! are all do the same just like i do ? !

I had to do this on one of my big games… it did fix all the memory issues.

But this isn’t a common way of handling memory.

okey thanks . please let me know if you know some good way of handling resource memory :smile:

@bilalmirza have you found any better way ? for handling resource memory.

This is a 4 year old topic. What are you struggling with where this topic should be bumped?

memory of my game is growing after each replace scene. :frowning: