Consider the following example:
function Process()
local Container=NewContainer()
Container:On(EventType.Add,function()
Container:DoSomething()
end)
-- Does not Garbage Collect
end
In luabridge, I store the function()
as LuaRef
which extends the lifetime for the Container
and it will not be GCed because it's a RefCountedObjectPtr
Here is a workaround that I use to use a weak table which works, but it looks ugly:
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
Is there any way to have a LuaRef
that functions similar to this? Or maybe there is another workaround?
Here is the way I approached this problem:
class Display.A()
in C++, create class A()
in Lua)self.WeakTable={}
and setmetatable(self.WeakTable, { __mode = 'k' })
)self.WeakTable.self=self
)self.WeakTable
to C++ and store in as LuaRef
- this will gcCreate a wrapper function like so:
Container:On(EventType.Add,function(WeakTableParams)
WeakTableParams.self.Callback();
end)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With