"Niling" out variables and references to those variables

local variablemain = {"table-fied"}  
  
local function test()  
local varableref = variablemain  
--etc--  
end  

Later on:

variablemain = nil  
  

Would setting variablemain equal to nil also nil out variable ref? [import]uid: 73951 topic_id: 23996 reply_id: 323996[/import]

Preface: I don’t know for certain.

…But I’m guessing not, since what is really happening there is that…

varableref = variablemain

…even if it’s nil, it’s still a reference and my guess is it won’t get trash collected.

But I could be wrong. [import]uid: 41884 topic_id: 23996 reply_id: 96695[/import]

I know what you’re asking, but I think you’ve got the example wrong. Only tables and functions get referenced from other variables. Other types such as strings and numbers simply get “copied” over to the other variable.

Here’s a modified version of your example to illustrate my point:

local variablemain = 5 local function test() local varableref = variablemain --etc--end[/code]And later on...variableref = 10print( variablemain, variableref ) -- 5 10[/code]Unless it's a table, setting one variable equal to another is like saying: "Set the value of 'variableref' to be the same as the value of 'variablemain'."If variablemain was a table (or function), however, then the statement would be: "Set the value of 'variableref' to be a reference to the table 'variablemain'."So to answer your original question, assuming that variablemain is a table, here's an example:local variablemain = { "hello world" } local function test() local varableref = variablemain --etc--end[/code]And then later on (somewhere within the scope of that block since variableref is local)...variableref = nilprint( variablemain[1] ) -- "hello world"[/code]As you can see, when we nil'd out variableref (which was a reference to the table variablemain), only the REFERENCE is removed, not the actual table. When we print out the value within the variablemain table, we still get the value we set so the table still exists.So.... long story short: you need to actually set variablemain to nil to get rid of it :-)Caveat: If variablemain is local, the block of code is finished executing, and there are no external references to it, then you don't have to set it to nil. Here's an example:local function doFunction() local variablemain = { "hello world!" }enddoFunction()[/code]In the "doFunction()" block (shown above), you don't have to set the variablemain table to nil, because it is local to its block of code, and no other variables reference it. [import]uid: 52430 topic_id: 23996 reply_id: 96748[/import]

@mr. beebe

I apologize for using an incorrect example, I honestly typed out my question very quickly (as I was on my way out the door! I remembered your article explaining the difference between the treatment of strings and numbers vs. tables when it was too late.) I have edited my example to include a table.

For the purposes of garbage collection , assuming I have external references to a variable, must I nil out BOTH the reference and the main variable?

What’s the case when dealing with numbers and strings in general, should they be removed?

To give some context to my questions: I’ve been having difficulty remembering what exactly I need to remove during scene transitions, particularly, in the case of variables.

I know display objects must be removed, runtime listeners, timers, audio stopped, physics stopped, std. listeners (I assume are removed with their respective display objects?) .
Once again, thank you for your help. Believe it or not, you’re one of the people who inspired me to actually take a crack at CoronaSDK. Keep up the tutorials. I really appreciate your attention to detail.

PS: I submitted a bug report regarding the transition issue I was having (similar to the snapshot bug I discussed with you in the past). {case #13201} [import]uid: 73951 topic_id: 23996 reply_id: 96754[/import]

Note: I understand that variables can be attached to many things, such as display objects. I am just trying to crack the issues surrounding multiple references to one thing and removal. [import]uid: 73951 topic_id: 23996 reply_id: 96759[/import]

Thanks for the kind words, mort :slight_smile:

To answer your question regarding non-table (or function) variables, really the only thing you need to worry about is global variables, as local variables will be collected once the block of code they are in is finished executing.

Global variables, however, are never collected until you set them to nil. This is the primary reason why I usually advise against globals. Performance improvements aside, it has nothing to do with globals themselves, and everything to do with the fact that it’s very easy to lose track of global variables if you’re not careful with using them.

Below is an example scene module that uses local variables. Afterwards, I’ll explain which variables would get collected on their own, and which one’s you’d likely want to nil out yourself when you’re finished with them.

examplescene.lua

local storyboard = require "storyboard"local scene = storyboard.newScene()local var1 = "string variable"local var2 = 100function scene:createScene( event ) local group = event.view local var3 = "another string variable" local var4 = 7777 print( var1, var2, var3, var4 )endscene:addEventListener( "createScene", scene )return scene[/code]In the example above, when a "createScene" event is dispatched and the listener function is called, var3 and var4 do not have to manually be nil'd out. They are local variables that are self-contained within the function's block of code. When that function is finished executing, var3 and var4 are collected safely.var1 and var2 are also local variables. However, the "block" they live in is the entire scope of the module (with the exception of the few lines above their definition). var1 and var2 are also known as "local globals". As long as the module is loaded into memory (e.g. until the scene is removed), these variables (along with their contents) also remain until (1) the module is removed and "unrequired" or (2) they are nil'd out manually.Therefore, if it's safe to get rid of the data that var1 and var2 holds, then you might consider nil'ing out those variables during an "exitScene" or "destroyScene" event. However, doing that is pointless if you plan on calling storyboard.removeScene() on the scene because in that case, the entire module (which include its local variables) will be garbage collected.The above also applies to local variables that are references to tables and functions. For instance, if you have a small block of code, a function lets say, which has a local variable that references an external table or function (an upvalue or global), you wouldn't need to nil out that local variable reference as it will be collected once the block of code that it "lives" in is finished executing. [import]uid: 52430 topic_id: 23996 reply_id: 96799[/import]

Your explanations make a heck of a lot of sense. Thank you so much. I am glad I double checked with you. It turns out I had nothing to worry about! [import]uid: 73951 topic_id: 23996 reply_id: 99035[/import]