Composer questions

If I forward-declare a variable, do I still need to ‘nil’ it even when I’m calling composer.removeScene(“scene”)

e.g.

[lua]local composer = require( “composer” )

local scene = composer.newScene()

local myVariable = 100

– “scene:create()”
function scene:create( event )

   local sceneGroup = self.view

– “scene:show()”
function scene:show( event )

   local sceneGroup = self.view
   local phase = event.phase

   if ( phase == “will” ) then

      elseif ( phase == “did” ) then

   end
end

– “scene:hide()”
function scene:hide( event )

   local sceneGroup = self.view
   local phase = event.phase

   if ( phase == “will” ) then

   elseif ( phase == “did” ) then
       myVariable = nil

       composer.removeScene(“scene”)
   end
end

– “scene:destroy()”
function scene:destroy( event )

   local sceneGroup = self.view

end


– Listener setup
scene:addEventListener( “create”, scene )
scene:addEventListener( “show”, scene )
scene:addEventListener( “hide”, scene )
scene:addEventListener( “destroy”, scene )


return scene[/lua]

No not as long as it is declared local.

Scene modules have their own scope.

Ok, so as long as I don’t set the recycle to true e.g. ‘composer.removeSCene(“scene”, true)’, local variables will get destroyed and the memory allocated for them will be freed up?

I think your ‘myvariable’ variable does need to be nilled because it is declare as local to the module, not the scene. Forward declaration has nothing to do with it. I do not believe that removeScene will unload the module - it will simply remove the scene object from memory. The module (the actual object created when the *.lua file is loaded into memory) is not unloaded.

It would be great to have this confirmed by Rob or Brent.

Composer.removeScene() does unrequire the module unless you pass “true” as the second parameter, which will just remove the scene’s view.

For what it’s worth…

local someVariable = 10

Is a simple variable.  It is not allocated memory.  Nilling it won’t save much if any memory.  Tables and things like audio and display can use big chunks of memory and its important to free those up.

Rob

Even if they are declared as local?

If they are local to a function they should be  disposed up.  If a scene gets un-required it should get freed.  Though any allocated texture memory would not be freeded up, though if your display objects are in a scene’s view group, that would get freed, but anything not in the group, audio, etc.

Rob

No not as long as it is declared local.

Scene modules have their own scope.

Ok, so as long as I don’t set the recycle to true e.g. ‘composer.removeSCene(“scene”, true)’, local variables will get destroyed and the memory allocated for them will be freed up?

I think your ‘myvariable’ variable does need to be nilled because it is declare as local to the module, not the scene. Forward declaration has nothing to do with it. I do not believe that removeScene will unload the module - it will simply remove the scene object from memory. The module (the actual object created when the *.lua file is loaded into memory) is not unloaded.

It would be great to have this confirmed by Rob or Brent.

Composer.removeScene() does unrequire the module unless you pass “true” as the second parameter, which will just remove the scene’s view.

For what it’s worth…

local someVariable = 10

Is a simple variable.  It is not allocated memory.  Nilling it won’t save much if any memory.  Tables and things like audio and display can use big chunks of memory and its important to free those up.

Rob

Even if they are declared as local?

If they are local to a function they should be  disposed up.  If a scene gets un-required it should get freed.  Though any allocated texture memory would not be freeded up, though if your display objects are in a scene’s view group, that would get freed, but anything not in the group, audio, etc.

Rob