考虑以下示例:
function Process()
local Container=NewContainer()
Container:On(EventType.Add,function()
Container:DoSomething()
end)
-- Does not Garbage Collect
end
在 luabridge 中,我存储了function()
as LuaRef
,它延长了 the 的生命周期,Container
它不会被 GCed,因为它是RefCountedObjectPtr
这是我用来使用弱表的解决方法,但它看起来很难看:
function Process()
local Container=NewContainer()
local ParamsTable={ Container=Container }
setmetatable(ParamsTable, { __mode = 'k' })
Container:On(EventType.Add,function()
ParamsTable.Container:DoSomething()
end)
-- Garbage Collects fine
end
有什么办法可以实现LuaRef
类似的功能吗?或者也许还有另一种解决方法?