lua-users home
lua-l archive

Re: Userdata Lifetime Management (was Prevent userdata from being marked for garbage collection?)

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


Vinnie Falco <vinnie.falco@gmail.com> writes:
> - Lua lifetime: Managed completely by Lua
> - C++ lifetime: Managed completely by C++
> - Shared lifetime: Reference counted with your choice of compatible container.
I don't use LuaBridge, but I do similar stuff using SWIG.
One thing that I've found very handy is the ability to mark certain
functions/methods as _transferring_ an object from one "realm" to the
other. This meshes well with C++ because the concept of object
ownership is pretty important in many C++ libraries.
For instance, a C++ method which returns an object pointer, which
should thereafter be managed by Lua (so the last reference in Lua to
that object should cause it to be deleted) is a C++ -> Lua transfer.
[Obviously the C++ code should not keep any references to the object
in this case]
Conversely, a C++ method which accepts an a pointer to a C++ object,
after which the Lua reference should become invalid, and the object is
now "owned" by the C++ side is an example of a Lua -> C++ transfer.
[For more complicated mixed usage, smart pointers of some sort are
useful, of course.]
-miles
-- 
Immortality, n. A toy which people cry for, And on their knees apply for,
 Dispute, contend and lie for, And if allowed Would be right proud
 Eternally to die for.

AltStyle によって変換されたページ (->オリジナル) /