lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


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)
        func(lq)
        lq:release()
    end
    with(linksrv.GetAll(...), function(lq)
        for l in pairs(lq) do ... end
    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
by-reference.

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? 

-- tom
telliamed@whoopdedo.org