请按照我的意见输入(使用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