Error about releasing object when try to switch a scene to another.

I follow the tutorial about Breakout Game which includes Box2D http://www.raywenderlich.com/505/how-to-create-a-simple-breakout-game-with-box2d-and-cocos2d-tutorial-part-22, and of course I use cocos2d-x.
Anyway, when I try to switch the scene whenever the ball contacts with the bottom edge (see the near end of “When we hit the botom” section in that link). With the same code I ran into problem of releasing object within the scene calling replaceScene() function.

I captured a screenshot in debugging stack to provide you with more information. That kind of error happens all the time whenever the ball touch the bottom and try to switch the scene with replaceScene().

Note: The code is pretty much the same, and follow the same structure as the original tutorial, but if you want me to show any specific code please let me know.

Any suggestions are very welcome !


Screen Shot 2012-02-26 at 7.09.30 PM.png (84.3 KB)

I have found that by integrating Box2D elements into the game mapping through a particular scene, when I need to transition to another scene the problem of releasing objects occurs (i’m not sure which part of my Box2D causes this).

I can loose the problem by using transition with some delay, but eventually the problem will happen anyway.

So I decided to try out pushScene() function of CCDirector to push the Box2D scene into a stack, then I can go to another scene without problem (because no releasing or destructor function of that scene will be called). Then if I need to go back to the Box2D scene, I use popScene() function of CCDirector to pop the scene put onto the stack.

I don’t think it’s totally a benefit, the code should work in normal way. So I still want your help if anyone can give me some suggestions.

FYI: After found this problem, I found that onEnter() and onExit() are really useful functions to use for one time loaded of scene and initialize / reset values when go to it or go back to it later on. I believe this will reduce the time needed to load stuff, and memory usage.

Hi,

I had similar problem with errors appearing on replace scene.
In my situation I was trying to replace scene within scheduled function and this was a problem. When I moved replace scene code outside my scheduled function it started to work.

I hope it helps you :slight_smile:

Hey Igrek, thanks for a reply.

I have my replaceScene() code outside of the scheduled function, in fact it’s in another layer of a scene. I make sure everything okay by calling to unscheduleSelectors() and unscheduleUpdate() before going to replace a scene, but nonetheless even if I don’t do that my scheduled function won’t process anything as it already checks to process or not if in proper stage.

It gets the same error about releasing objects. I don’t know how to fix it.

FYI: I use another way around by pushScene() the current scene, so this will avoid releasing anything permanently. But when I go back to it again I create a new scene which not good approach at all but it works and after gone through the memory leak analysis I don’t see any thing suspicious which is quite shock ! Anyway, I don’t like my approach at all.