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? [import]uid: 49842 topic_id: 11544 reply_id: 311544[/import]

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:

[lua]local newVar = myVar + 1[/lua]

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

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

But; [lua]function( age )[/lua] will pass the value of age to local variable myVar, but not the age variable itself.
[import]uid: 11393 topic_id: 11544 reply_id: 41905[/import]

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. [import]uid: 49842 topic_id: 11544 reply_id: 41911[/import]

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. [import]uid: 26 topic_id: 11544 reply_id: 41941[/import]

thanks, all clear now! [import]uid: 49842 topic_id: 11544 reply_id: 41945[/import]