Composer Scene Objects - Not removed while we remove the scene from memory

Hi.  I answered a similar question the other day so I just amended my example for that question.

You can download two examples here:

https://github.com/roaminggamer/RG_FreeStuff/raw/master/AskEd/2016/12/correct_management.zip

  • question1_161216 - Creates and destroys semi-complex content in a scene over and over using will/did mechanisms, then looks for memory leak.
  • question2_161223 - Using create + did hide + removeScene.  Again, looks for leaks.

Warning: To run it directly you will need SSK2 PRO or lite.

However, you can read the code and see what I’ve done. Pay particular attention to scene2.lua.

(Also, this code is easily modified to not use SSK2 if you want to use it for your own example.)

I just ran the  question2_161223  example for 10 iterations with Corona SDK 2016.3001 and observed no leaks.

-Ed

https://www.youtube.com/watch?v=bE1z5n3NphA

Let me chime in here. Composer by default caches scenes so that it’s more efficient the next time you visit it. Hiding the scene does just that, it “hides”’ the scene. It does not “remove” the scene.  That’s the job of the composer.removeScene() function.

scene:create()'s job is to create the objects and put them into a bucket that composer can mange (the scene’s view group)

scene:show()'s job is to execute code when the scene show’s via composer.gotoScene(). Here you start up times and such after the scene is on the screen.

scene:hide()'s job is to execute code when the scene is being hidden (via a composer.gotoScene() call). Here you stop things you started in scene:show().

If you want to clear your memory of the objects, you must call composer.removeScene() to remove those display objects. You do not have to remove them yourself. Just call composer.removeScene(“scenanemtoremove”).  The one gotcha is you can’t remove the scene that’s on the screen.

Rob

I wanted to add more to this too.

When you call composer.gotoScene here is the basic logic:

Does a view for the scene exist? No call scene:create() for the scene

Call the current scene’s scene:hide() will phase.

Call the destination scene’s scene:show() will phase.

Transition the two scenes

Call the now previous scene’s scene:hide() did phase

Call the now current scene’s scene:show() did phase

Then for composer.removeScene():

Call the scene’s scene:destory() function.

Remove all of the display objects for the scene.

Rob