lua-users home
lua-l archive

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


I tidied up my thoughts on this.

One thing I couldn't quite express in my last mail is about the "remove key after finalization" and "remove value before finalization" behavior. For example, when a weak value is removed from a table before its finalization, so is the corresponding key (logically). But what if the key requires this entry to be removed after finalization?
What is the rational for the removal behavior to have to be different for keys and values? Wouldn't it be ok for the values to removed later, too?

--

While doing my little experiments with the garbage collector, I thought that similar to ephemeron tables a weak value should control the reachability of its corresponding key.
Though counterintuitive, right now the key is collected/finalized one garbage collector cycle later than the value, even though the whole entry is removed before the finalizer of the value is called. This could cause the garbage collector to ripple through further tables like that.
What do you think?

--

I also found a bug maybe.
In a table with weak keys and an entry in the form of a closure as the key with one of its captured values as the value (and neither reachable), the entry is removed before the finalizer is called. I guess that is not correct?
Swap the key and value and it is removed afterwards.
Did I miss something here?

collectgarbage"stop"
do
local v = setmetatable({}, {__gc = function () for k, v in pairs(t) do print(k, v) end end})
local function f() return v end
t[f] = v
end
collectgarbage()

--

On the topic in the title, I found out that C# / .NET have the functions GC.SuppressFinalize and GC.ReRegisterForFinalize. Couldn't Lua benefit from explicit functions instead of the indirect way through setmetatable?

--

I still have several other questions on my mind, like how to deal with accessing finalized objects (e.g. in memoization tables), but one thing after the other : )

Please join me in my monologue about the garbage : D