lua-users home
lua-l archive

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


> Here's another issue I encountered with __gc: it appears that userdata
> created inside a finalizer does not have its finalizer called.  This
> leaks any resources associated with the userdata.  This test prints
> "Created" but not "GC'd" in both 5.1 and 5.2.

It does not seem a good practice to create new resources when finalizing
something, and it can easily create an infinite loop. So, to avoid
infinite loops, Lua does not call the finalizers of objects created
after the call to lua_close. This behavior only happens when closing a
state, and it is intentional (even if not documented).

On the other hand, this behavior allows some nice tricks. For instance,
the Lua test uses something like this to track garbage collections:

do
  local mt = {__gc = function (o)
    io.stderr:write("+");
    setmetatable({}, getmetatable(o))
  end}
  setmetatable({}, mt)
end


-- Roberto