[Date Prev][Date Next][Thread Prev][Thread Next]
- Subject: Alternatives to reference counting
- From: TNHarris <telliamed@...>
- Date: Tue, 26 May 2009 20:27:37 -0400
It's the old problem of needing to release an application resource when
it goes out of scope. A link query holds a global lock so I can't wait
for the userdata to be collected.
The reliable way is to explicitly call a release function, which is what
I'm currently doing, with a bold-faced note reminding me to always
release link queries. But I'm sure one day I (or one of my users) will
forget. So an automatic release would be preferred.
Could the gc be tuned to do it? But that would require running a full
cycle after every instruction, wouldn't it? Not a good idea.
I could do something like this:
function with(lq, func)
for l in pairs(lq) do ... end
Not the prettiest thing to type. May as well use MetaLua then, even
though that complicates deployment of the plugin.
So all I'm left with is adding a reference count to the VM and call a
'__final' metamethod when the userdata goes out of scope.
And there's another issue that may need a reference count. The event
data for an application callback is passed as one of many structures. I
just stick this pointer into a userdata and attach a metatable based on
the event type. It works well as long as the userdata doesn't get stored
in a global variable or closure, or inside a table referenced by a
global or closure. The pointer to the event data becomes invalid after
the callback returns. In this case I think I need a '__copy' metamethod.
I'm not sure about the best way to do that. Is it enough to just invoke
__copy when writing the object to a table or closure? So all stack
manipulations (local variables, function call arguments) are
I'm looking at the luarc from LuaPlus. But I don't want to replace the
GC like that seems to do. And it's only for Lua 5.0. Or is reference
counting the wrong approach?