lua-users home
lua-l archive

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




2011/6/27 steve donovan <steve.j.donovan@gmail.com>
On Mon, Jun 27, 2011 at 11:08 AM, Gaspard Bucher <gaspard@teti.ch> wrote:
> collectgarbage() ---- does not collect old "foo". Why ? Where is it stored ?
> print('end') -- the garbage collection after 'end' removes old "foo".

It's still there, as a global of the original environment.

steve d.

Yes! this version worked:

-- cc: cc='lua52'
local print = print
local collectgarbage = collectgarbage
local setmetatable = setmetatable
local gc_mt = {}
function gc_mt.__gc(obj)
  print('gc', obj.name)
end

_ENV = {}
foo = {name = 'foo'}
setmetatable(foo, gc_mt)

_ENV = {}
print(foo)     ---- nil as expected

print"first"
collectgarbage() -- collect here
print"second"
collectgarbage()

print('end') -- the garbage collection after 'end' removes old "foo".