lua-users home
lua-l archive

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

> What if you increment your object's refcount  by one when you first pass
> it to Lua to take into account that  Lua has a reference to it? Lua's gc
> would then  only need to decrement  the refcount by one.

I do something identical to this in a project; the userdata pointer in Lua
is exactly the same as any C++ reference to the object in the way it is
reference counted.  As you say, the gc just decrements the count.  This
also works well with multiple Lua references to the same object via
different userdata objects (in 4.1), as each different one just holds a
ref on the object.

I don't have the requirement of C++ having a Lua table, as in this example,
but I can't see why that wouldn't work more or less the same way; Lua maintains
the refcount, C++ can effectively hold a ref as long as it likes, and release
it when it wants.

Love, Light and Peace,
- Peter Loveday
eyeon Software