[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: RE: Issues with the garbage collector
- From: "Jerome Vuarand" <jerome.vuarand@...>
- Date: Thu, 30 Nov 2006 18:43:58 -0500
Wesley Smith wrote:
> As a quick follow up just to be more precise, what's the
> difference between this:
> --------------script global scope
> function cb(event)
> print(event)
> end
>
> myvar = 1
> local myvar2 = 1
> --------------end script
>
> I load this script with a loadstring and pcall and everything is fine.
> Now, I have in C a callback mechanism that does a getglobal
> on cb and passes in some args. In the process, I create a
> table, so eventually the GC gets called. When that happens,
> myvar2 will disappear and obviously myvar won't. It was my
> (apparently wrong) understanding that local values at global
> scope would persist at the global level.
> However, this isn't the case. Where do they exist and to
> what extent do they exist?
A script is a chunk, and its like any anonymous function. A global
environment is en entry in the function/chunk/script environment table.
A local variable only exist during the execution of the
function/chunk/script. So local variables are collectable as soon as the
function/chunk/script has been executed, whereas global variables are
collectable only when the environment table is collected. If the
environment table is the global one (_G), which is the default, then
they are not collectable before you close the Lua interpreter.