lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On Jan 21, 2014, at 8:25 PM, Andrew Starks <andrew.starks@trms.com> wrote:

> msg = function(a, b, c)
> 
>   return function()
>     return a, b, c
>   end
> end
> 
> 
> Let's say that I call this a bunch of times, sending it as a "message" to another function, which may store them in a cue, for later processing. 
> 
> Are the instances of "a, b, c" on the heap (garbage collected) or in the stack?
> 
> This may be a dumb question. I apologize if it is. 
> 
> -Andrew
> 

They are garbage collected. Roberto did an interesting paper a while back on how Lua handles upvalues. As I recall it, they start on the stack and are migrated out of the stack when the declaring function (not referencing function) goes out of scope. So when your outer function returns, they logically move from the stack to the heap. Remember, though, that things on the stack can be GCed too; in languages like Lua the distinction between the stack and heap is less clear-cut that C or C++.

—Tim