Jump to content

[TOPIC: topicViewTemplate]
[GLOBAL: userSmallPhoto]
Photo

question regarding scene cleaning?
Started by dodi_games Sep 27 2018 12:04 PM

6 replies to this topic

Best Answer roaminggamer , 28 September 2018 - 08:18 AM

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

[TOPIC CONTROLS]
[/TOPIC CONTROLS]
[modOptionsDropdown]
[/modOptionsDropdown]
[reputationFilter]
[TOPIC: post.html]
#1

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 324 posts
  • Corona SDK

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 )

nil is printed on the console



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?



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



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



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



Thanks


Dodi



[TOPIC: post.html]
#2

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

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()

 

3. 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. 

5. 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.


Edited by roaminggamer, 28 September 2018 - 08:19 AM.


[TOPIC: post.html]
#3

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

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


Edited by roaminggamer, 27 September 2018 - 12:20 PM.


[TOPIC: post.html]
#4

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 324 posts
  • Corona SDK

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

[TOPIC: post.html]
#5

dodi_games

[GLOBAL: userInfoPane.html]
dodi_games
  • Contributor

  • 324 posts
  • Corona SDK

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

[TOPIC: post.html]
#6

roaminggamer

[GLOBAL: userInfoPane.html]
roaminggamer
  • Corona Geek

  • 7,363 posts
  • Corona SDK

  Best Answer

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


  • dodi_games likes this

[TOPIC: post.html]
#7

Rob Miracle

[GLOBAL: userInfoPane.html]
Rob Miracle
  • Moderator

  • 24,969 posts
  • Corona Staff

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


  • dodi_games likes this


[topic_controls]
[/topic_controls]