lua-users home
lua-l archive

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


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?

thanks,
wes



On 11/30/06, Wesley Smith <wesley.hoke@gmail.com> wrote:
function cb(event)
        print("cb: " .. tostring(event))
        for k, v in pairs(event) do
                print(k)
                print("\t" .. tostring(v))
                print("")
        end

        print(table.concat(event.args, ", "))
        print(listener)
end

--userdata
local listener = jit.listener("listen", "cb")
local xfade = jit.new("jit.xfade")

In the above script, I have 2 userdata variables.  The "listener"
object registers a Lua function as a callback from another part of the
app the script is embedded in.  In the C function that passes data to
the Lua callback function, I'm allocating a small table, "event", and
passing it to the callback function.  After a certain period of time,
the garbage collector gets called and calls both userdata __gc
functions, freeing them.

My question  is how does the GC decide what to free?  I don't think
either listener or xfade should be deleted because they're still valid
variables.  What is the metric being used by Lua to decide what gets
deleted?

thanks,
wes