Re: Preventing garbage collection of objects created with Luabind

Nigel Atkinson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <1267066140.2729.18.camel@finwe>
I third the smart pointer option.  I use them extensively throughout my
code both when dealing with Lua and in general.

So you would have something like (typedefs just make it pretty):

class Component;
typedef boost::shared_ptr<Component> ComponentPtr;
typedef std::vector<ComponentPtr> ComponentSet;

etc;

You bind this like:

class_<ComponentSet>( "ComponentSet" )
// rest it like you did before....

class_<Component, ComponetPtr>( "Component" )
// as before...

and the tricky one...

class_<Relay, Component, ComponentPtr>( "Relay" )
// Note that it uses the shared pointer type of the base.

Of course if you are new to smart pointers, you will want to do some
reading.  There are some gotcha's, but if you know them, you can avoid
them.

Nigel

On Thu, 2010-02-25 at 12:54 +1100, Nikolas Bowe wrote:
> you need to keep a reference somewhere in lua
> 
> you can use the registry
> or you can look at wrap_base
> or you can look at transferring pointer ownership between Lua and C++
> (ie you could have push_back adopt the pointer into C++)
> you can also try a smart pointer holder like tony suggested. (that way
> lua just destroys the smart pointer instead of the whole object)
> 
> we use smart pointers and/or wrappers (wrap_base) for most of our
> stuff..
> we have very few cases of registry stashing and few cases of manual
> ownership transfer.
> 
> 
> 
> Greg Santucci wrote: 
> > 
> > Hi Luabinders,
> > 
> > I've registered some functions of an stl vector using Luabind the
> > following way without any problems:
[...]



------------------------------------------------------------------------------
Download Intel&#174; Parallel Studio Eval
Try the new software tools for yourself. Speed compiling, find bugs
proactively, and fine-tune applications for parallel performance.
See why Intel Parallel Studio got high marks during beta.
http://p.sf.net/sfu/intel-sw-dev
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.