variables/memory

Quick question: Do I have to nil out all variables after use? Do they hold onto memory?

e.g

function something ( myVar )

newVar = myVar + 1
.... more code here

myVar = nil -- is this necessary?
newVar = nil - is this necessary?
end

or do they die at the end of a function anyway?

If they are not in a function, do I need to destroy them as well?

Newvar is declared inside the function but is declared as global variables so you will have to nil it out after use to free the memory.

What you need to do is add the local declaration in front of the variable name:

local newVar = myVar + 1

newVar will be nilled after the function call is complete along with myVar as both will be local variables.

So; something( 1 ) with the changes above will be fully cleaned up.

But; function( age ) will pass the value of age to local variable myVar, but not the age variable itself.

thanks
sorry, I meant declared as local but forgot to type it.

so, everything inside the function would be cleaned up automatically.

and if I understand you correctly, variables declared outside of a function do need to be nilled out.

well, everything in that function that's a local variable would be cleaned up --- unless you have a global variable referencing it, stuck it in a table, referred to it in a closure, etc.

thanks, all clear now!

views:1527 update:2011/10/5 21:23:48
corona forums © 2003-2011