lua-users home
lua-l archive

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


Hi, list

Here are the related code snippets(for your convenient, see ):
mt = {__gc = function (o) print("gc:",o[1], o[2]) end}

--[[
list1 = {1, nil  }
list2 = {2, list1}
list3 = {3, list2}
list4 = {4, list3}
list5 = {5, list4}
--]]
list = nil
for i = 1,5 do
list = setmetatable({i, link = list}, mt)
end

::BEGIN::
for k,v in pairs(list) do
print("key=",k,"value=",v)
if(k == "link") then
print "==========="
if(v ~= nil) then
list = v
collectgarbage()
goto BEGIN;
end
end
end

Here are the outputs:

key= 1 value= 5
key= link value= table: 0x55b1c39c0630
--Question:
--                Why does not the finalizer been called here after
"list = v" has been invoked?
--                For the former variable "list" is still referenced
by "k,v in pairs(list)"?
--                If the guess aforementioned is right, why does not
the finalizer been called in the next loop?
===========
key= 1 value= 4
key= link value= table: 0x55b1c39c8e30
===========
gc: 5 nil
key= 1 value= 3
key= link value= table: 0x55b1c39c85e0
===========
gc: 4 nil
key= 1 value= 2
key= link value= table: 0x55b1c39c8860
===========
gc: 3 nil
key= 1 value= 1
gc: 2 nil
gc: 1 nil

Thank you for your attention to my question.
Best regards.

Sunshilong