lua-users home
lua-l archive

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

On Mon, Dec 28, 2020 at 8:24 AM Thijs Schreijer <> wrote:
> First one finalises (calling GC metamethods), 2nd call actually cleans them out

That's assuming you don't write a __gc metamethod that does this:

metatable = {
    __gc = function(object)
        someGlobal = object

In this case the in object will not be collected until something
modifies "someGlobal" or the program terminates.

The finalizer will only run once for each object, unless you use
"setmetatable" again, which creates an object that is unreachable but
can never be collected, it's a zombie object of sorts:

local mt; mt = {
    __gc = function(ob)
        setmetatable(ob, mt)

local object = setmetatable({}, mt)

object = nil
while true do
    -- generate lots of trash to keep the garbage collector busy
    local trash = ("xxxxx"):rep(1000000)

There are plenty of ways to shoot yourself in the foot with __gc.