Re: Preventing garbage collection of objects created with Luabind

Greg Santucci <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
Actually disregard my last post!! The problem is solved, thanks to all your
help. I discovered I don't need to use my GetPtr() function anymore (GetPtr
was updated to return the ComponentPtr smart pointer type).

When I was doing this I had the problem:

> c:push_back(Relay():GetPtr())

I discovered I no longer needed to use GetPtr() (lua is happy with what
Relay() provides, for whatever magical reason that I would like to
investigate):

> c:push_back(Relay())

Now I can call this all day long, without it being destroyed:

> print(c:at(0):Speak())
Relay

And when my app closes, the smart pointer deletes all remaining Component
instances.

Thanks again!

Greg


On Thu, Feb 25, 2010 at 3:58 PM, Greg Santucci <[email protected]>wrote:

>
> Thank you. I tried incorporating boost shared pointers the way you
> describe, but when lua garbage collects, its still deleting the components.
> (I confirmed that the Relay destructor was being called through some
> shared_ptr boost code.) Is there a step I've missed which would have bumped
> up the use count?
>
>
> On Thu, Feb 25, 2010 at 1:49 PM, Nigel Atkinson <[email protected]>wrote:
>
>> 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
>> _______________________________________________
>> luabind-user mailing list
>> [email protected]
>> https://lists.sourceforge.net/lists/listinfo/luabind-user
>>
>
>

------------------------------------------------------------------------------
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

_______________________________________________
luabind-user mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/luabind-user
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.