functions and local variables... please help

I have several important questions regarding lua 

first

I have always wondered if you always have to release the local variables by using nil

for example , I have a code where a local variable is made within a function

local realx = 10

local function dosth()

     local x = 13

     realx = x

    --(x = nil) is this necessary???

end

second

 in C a function is usually ended when return is called however in lua it doesn’t seem like it

when I make a function like this 

local function dosth()

      local count = 0

      timer.performWithDelay(1000,function() print(count…“seconds”) end,50)

      return 0

end

I belive the local variable count still remains in memory even after return is called how does this work?

  1. No. In any language, when the code execution leaves the scope of a given variable, the variable is released from memory. This is due to “garbage collection” when check through the list of objects in memory for which there are no more references pointing at them. If a block of memory has no references, it is returned to the “heap.”

The only exception to this is if you create a “closure”, which is possible in functional languages such as Lua and F#. Because you can create objects which remain in memory even after the function which created them has ended, their “scope” is no longer present, but they will not be removed. This is not a problem as long as you eventually remove all of your variables and thus end your closures. This is not the same as referencing a function as a first class member.

1.1. No, in your code setting x to nil is not necessary because you have declared it as local. Declaring it as local means the function it is declared within provides the scope of the variable. Once the function (and thus the scope) ends, the variable will be garbage collected (freed up.)

  1. Correct. This is because of closures (citation may be needed by another forum user.) The timer is still executing and the function that the timer calls contains a reference to the variable ‘count’, and so the ‘count’ variable will not be garbage collected. In short, this is because in any managed memory environment (.NET, Lua, Java, etc.) any piece of memory which is referenced by a variable will not be freed up. The function uses the ‘count’ variable and so there is still a reference to the variable and thus it will not be garbage collected. Once the function being called by the timer is no longer called (because the timer ends) the ‘count’ variable will cease to exist and it’s memory will be freed up.

One rule: Always use local within functions.

There’s more, of course.

http://www.lua.org/pil/17.html

Oh, there’s some really great information here, too:

http://www.coronalabs.com/resources/videos/

  1. No. In any language, when the code execution leaves the scope of a given variable, the variable is released from memory. This is due to “garbage collection” when check through the list of objects in memory for which there are no more references pointing at them. If a block of memory has no references, it is returned to the “heap.”

The only exception to this is if you create a “closure”, which is possible in functional languages such as Lua and F#. Because you can create objects which remain in memory even after the function which created them has ended, their “scope” is no longer present, but they will not be removed. This is not a problem as long as you eventually remove all of your variables and thus end your closures. This is not the same as referencing a function as a first class member.

1.1. No, in your code setting x to nil is not necessary because you have declared it as local. Declaring it as local means the function it is declared within provides the scope of the variable. Once the function (and thus the scope) ends, the variable will be garbage collected (freed up.)

  1. Correct. This is because of closures (citation may be needed by another forum user.) The timer is still executing and the function that the timer calls contains a reference to the variable ‘count’, and so the ‘count’ variable will not be garbage collected. In short, this is because in any managed memory environment (.NET, Lua, Java, etc.) any piece of memory which is referenced by a variable will not be freed up. The function uses the ‘count’ variable and so there is still a reference to the variable and thus it will not be garbage collected. Once the function being called by the timer is no longer called (because the timer ends) the ‘count’ variable will cease to exist and it’s memory will be freed up.

One rule: Always use local within functions.

There’s more, of course.

http://www.lua.org/pil/17.html

Oh, there’s some really great information here, too:

http://www.coronalabs.com/resources/videos/