lua-users home
lua-l archive

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


I manage all C++ objects accessible to Lua with boost::shared_ptr.  The Lua
userdata to a C++ object is just a shared_ptr to the C++ object.  I never
explicitly destroy anything in C++ and it all cleans up automagically.

In this system, a C++ object *won't* be destroyed if it is referred to be
Lua - you never have to test for validity in Lua code.  C++ objects act like
GC objects.

Note: shared_ptr is a reference-counted memory manager, not a GC system, so
you have to think through potential circular references.  For example: If a
C++ object referenced by Lua userdata owns a reference to a Lua thread...

-Erik


> -----Original Message-----
> From: lua-bounces@bazar2.conectiva.com.br [mailto:lua-
> bounces@bazar2.conectiva.com.br] On Behalf Of Wesley Smith
> Sent: Wednesday, February 14, 2007 10:32 PM
> To: Lua list
> Subject: Lua/C++ bidirection free
> 
> Hi Collective Wisdom,
> I'm struggling with a design issue in integrating Lua into a C++
> library.  The way the library is designed, the destructor of an object
> can create a cascade of destructor calls some of which could
> potentially be resources referred to in Lua.  The problem I'm trying
> to solve right now is how to manage freeing userdata in Lua once a C++
> object having nothing to do with Lua destroys it.  Are there idioms
> people use for dealing with this kind of thing?
> 
> thanks,
> wes