question regarding scene cleaning?

It is necessary to remove all local references?

ex: 1

--outside scene:create() local square --scene:create() square = display.newRect( 0, 0, 150, 50 ) sceneGroup:insert(square) --scene:hide() ……… did phase print( square )

a table is printed on the console

15:49:50.798 table: 0A079AF8

ex: 2

--scene:create() local square = display.newRect( 0, 0, 150, 50 ) sceneGroup:insert(square) --scene:hide() elseif ( phase == "did" ) then print( square )

[font=‘lucida sans unicode’]

[font=‘Helvetica Neue’]

nil is printed on the console

[/font][/font]

[font=‘lucida sans unicode’]

[font=‘Helvetica Neue’]

I understand that all local variables created in scene create are part of this big function and gets destroyed in scene:destroy( event ), so square is a global variable without any assignment in ex: 2 but what about the other variables?

[/font][/font]

[font=‘lucida sans unicode’]

[font=‘Helvetica Neue’]

Do I need to remove all display objects references and nil all variables in scene destroy? for example local centerX, etc…

[/font][/font]

[font=‘lucida sans unicode’]

I understand the timers and transition manualy removal but what about the other locals?

[/font]

[font=‘lucida sans unicode’]

[font=‘Helvetica Neue’]

It is a silly question but it has caused me doubts.

[/font][/font]

[font=‘lucida sans unicode’]

[font=‘Helvetica Neue’]

Thanks

[/font][/font]
[font=‘lucida sans unicode’]

[font=‘Helvetica Neue’]

Dodi

[/font][/font]

What you’ve said isn’t actually true:
 

I understand that all local variables created in scene create are part of this big function and gets destroyed in scene:destroy( event ), so square is a global variable without any assignment in ex: 2 but what about the other variables?

  1. Nothing is done to file-level local variables by composer unless you write code to do something.
     
  2. Any local variable in a composer scene method is not visible or accessible in the other scene methods. 

For example, a local variable in the create() method is completely not visible in destroy()

  1. Do not conflate variables with objects.  A variable just refers to the object.  If composer manages that object it will destroy it in destroy()

 
4. I don’t see you adding those objects to the scene, so composer isn’t even managing them.
Saw your update. 

  1. This discussion would be easier to help you with if you would make a tiny project with two scenes, zip it up, and link it here or attach it here.

File-Level Local

Additionally, to answer your original question, if you do this:

-- at top of scene file local square -- create method function scene:create( event ) local sceneGroup = self.view square = display.newRect( sceneGroup, 0, 0, 150, 50 ) end

You must do this, to ensure the object is garbage collected.

-- destroy method function scene:destroy( event ) square = nil end

Scene Method Level Local

If you do this, there is no need to do anything special in destroy()

function scene:create( event ) local sceneGroup = self.view local square = display.newRect( sceneGroup, 0, 0, 150, 50 ) end -- square falls out of scope at end of create and is automatically removed (essentially set to nil) -- the variable.... not the object

Sorry @RG I make the correction, I insert the object to sceneGroup.

Do I need to do the File-level local example to all forward variables like Local centerX, local centerY, etc?

If you are asking if you need to clear all variables, then no.  Just clear variables that refer to display objects.

And remember Composer works for you. When you do:

local someObject = display.someDisplayObject()

sceneGroup:insert( someObject )

The local reference to someObject will go away when that function ends, but the allocated memory won’t. Also since you added it to the sceneGroup, scenGroup is holding a reference to the object.

This is key to Composer’s desire to cache scenes for you. Corona is designed for you to not have to recreate a scene when you go back to it. Scenes are only destroyed on low memory, instructing Composer to automatically remove scenes on change, or your intentional call to composer.removeScene(). By adding your object to the scene’s view, you’re making a decision to let Composer manage that object for you. When the scene is removed (either by a settings value, or by a call to composer.removeScene()), Composer will clean up those display objects for you.

Rob

What you’ve said isn’t actually true:
 

I understand that all local variables created in scene create are part of this big function and gets destroyed in scene:destroy( event ), so square is a global variable without any assignment in ex: 2 but what about the other variables?

  1. Nothing is done to file-level local variables by composer unless you write code to do something.
     
  2. Any local variable in a composer scene method is not visible or accessible in the other scene methods. 

For example, a local variable in the create() method is completely not visible in destroy()

  1. Do not conflate variables with objects.  A variable just refers to the object.  If composer manages that object it will destroy it in destroy()

 
4. I don’t see you adding those objects to the scene, so composer isn’t even managing them.
Saw your update. 

  1. This discussion would be easier to help you with if you would make a tiny project with two scenes, zip it up, and link it here or attach it here.

File-Level Local

Additionally, to answer your original question, if you do this:

-- at top of scene file local square -- create method function scene:create( event ) local sceneGroup = self.view square = display.newRect( sceneGroup, 0, 0, 150, 50 ) end

You must do this, to ensure the object is garbage collected.

-- destroy method function scene:destroy( event ) square = nil end

Scene Method Level Local

If you do this, there is no need to do anything special in destroy()

function scene:create( event ) local sceneGroup = self.view local square = display.newRect( sceneGroup, 0, 0, 150, 50 ) end -- square falls out of scope at end of create and is automatically removed (essentially set to nil) -- the variable.... not the object

Sorry @RG I make the correction, I insert the object to sceneGroup.

Do I need to do the File-level local example to all forward variables like Local centerX, local centerY, etc?

If you are asking if you need to clear all variables, then no.  Just clear variables that refer to display objects.

And remember Composer works for you. When you do:

local someObject = display.someDisplayObject()

sceneGroup:insert( someObject )

The local reference to someObject will go away when that function ends, but the allocated memory won’t. Also since you added it to the sceneGroup, scenGroup is holding a reference to the object.

This is key to Composer’s desire to cache scenes for you. Corona is designed for you to not have to recreate a scene when you go back to it. Scenes are only destroyed on low memory, instructing Composer to automatically remove scenes on change, or your intentional call to composer.removeScene(). By adding your object to the scene’s view, you’re making a decision to let Composer manage that object for you. When the scene is removed (either by a settings value, or by a call to composer.removeScene()), Composer will clean up those display objects for you.

Rob