は(JDoodleでLuaの5.3.2を使用して)私の入力のコメントに従ってください:ガベージコレクタは、デッドオブジェクトを収集するときに何をしますか?
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);
私はnil
でtable
を割り当てた後weakvalues[1]
を削除しますtable
でテーブルを考えました。
'v'は値です。しかしあなたの場合には 'k'が必要だと思います。 – moteus
@moteus' 'k''は弱い鍵です... – Hydro
申し訳ありません。あなたは正しいです – moteus