lua-users home
lua-l archive

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


> Here is a problem that I couldn't found any implementation about it in
> lua5.3.5 source code, Lua does nothing before/after calling the finalizers
> in c function GCTM (lgc.c).
> 
> In contrast, I found that in c function atomic (lgc.c), At each cycle, the
> collector clears the values in other weak tables before resurrecting all
> objects which has a finalizer, and the collector clears the keys after
> resurrecting these objects.

That is it. Lua only clears tables during GCs. When it says "before
calling the finalizer", it means in the GC cycle that finds that the
object is not accessible (and therefore must be finalized). When it says
"after calling the finalizer", it means in the GC cycle where the object
is actually collected. So, when the collector clears the keys after
resurrecting, those objects in the keys that were resurrected are not
cleared. They will be cleared only in the next cycle that collects them.


> I wrote such lua code for verification:
> 
> do
> 
> local k = {}
> local tbl1 = setmetatable({ [k] = "Hello" }, { __mode = "k" })
> local tbl2 = setmetatable({ Hi = k }, { __mode = "v" })
> local tbl3 = setmetatable({ tbl1 = tbl1, tbl2 = tbl2, k = k }, { __gc =
> function(obj)
>     for k, v in pairs(obj.tbl1) do print("tbl1:", k, v) end --> print k =>
> Hello
>     for k, v in pairs(obj.tbl2) do print("tbl2:", k, v) end --> print
> nothing
>     print("obj.k:", obj.k) --> print table k
> end })
> print("Start...")
> k = nil
> tbl3 = nil
> collectgarbage()
> for k, v in pairs(tbl1) do print("First GC tbl1:", k, v) end --> print k =>
> Hello
> for k, v in pairs(tbl2) do print("First GC tbl2:", k, v) end --> print
> nothing
> collectgarbage()
> for k, v in pairs(tbl1) do print("Second GC tbl1:", k, v) end --> print
> nothing
> for k, v in pairs(tbl2) do print("Second GC tbl2:", k, v) end --> print
> nothing
> print("END!")
> 
> end
> 
> Is it a description error in the book?(Programming in Lua)

What is wrong in that result? What would you expect?

-- Roberto