請按照我的意見輸入(使用Lua 5.3.2在JDoodle):垃圾收集器在收集死對象時會做什麼?
local table = {};
local weakvalues = setmetatable(
{
table
},
{
-- Let weakvalues values
-- be weak, so that they fade when dead objects are g-c.
__mode = 'v'
});
table = _;
-- Now the previously ref. table is unreachable
-- since there are no other references, I think so...
-- Sychronously wait this loop statements
-- in order to execute a next statement (maybe the
-- garbage-collector would have collected the unreachable table above
-- while this loop executes).
for i = 1, 5e7 do end
-- Expected to log 0, but it logs 1. Is the unreachable table
-- still reachable?
print(#weakvalues);
我想表在table
與nil
分配table
後會刪除weakvalues[1]
。
'v'是價值。但在你的情況下,你需要'k'我認爲 – moteus
@moteus''k''是弱鍵... – Hydro
對不起。你是對的 – moteus