From Rob Miracle’s “Cleaning up Display Objects and Listeners” :
"Be Careful…
Local references to functions are nil’ed out when the function ends. That is, they only live for the time that the function is executing. These will be cleaned up for you.
Where you can create a small memory leak is if you do this:
local enemy local function spawnEnemy() enemy = display.newImageRect( "badguy.com", 64, 64 ) return enemy end function scene:createScene( event ) local group = scene.view local background = display.newRect( 0, 0, display.contentWidth, display.contentHeight ) group:insert( background ) local doSomething = display.newImageRect( "button.png", 64, 32 ) group:insert( doSomething ) for i = 1, 100 do group:insert( spawnEnemy() ) end end
Similar to the first example, the enemies will all be cleaned up when Storyboard destroys the scene, however the “enemy” variable will stick around until the scene is “un-required.” "
1st:
What is meant by “local references to functions”?
Could you provide an example? ( Are we talking about forward declarations?)
2nd :
In the example above, are you suggesting (as a best practice) that all forward-declared items (including functions, etc.) should be nil’ed out upon scene exit ?
Or, is it just safe to wait until the module unrequired manually or automatically?
3rd:
When something (anything) is forward-declared, is it entirely prevented from being removed from memory, or is it just a leftover variable that needs to be nil’d?
i.e. , If Storyboard removes a forward-declared display object via its master group, would the forward declared variable be all that remains (and thus all that needs to be nil’d out)?