Discarding shared_ptrs

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Discarding shared_ptrs

Simon Pickles-3
Hi,

I am passing boost::shared_ptr<T> to Lua functions, which works well.

Unfortunately, the shared_ptr then keeps an extra use_count (held in the
lua table), even if the function is finished. How do I release this usage?

Set to nil? Use a Policy? Pass some other sort of pointer? I'd love to
be able to use the functionality of boost::weak_ptr in lua!

Any advice welcome

thanks

Simon



------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Discarding shared_ptrs

Nigel Atkinson-2
If you keep a reference around in Lua you will have a use count for it.
You can set to nil to remove that reference but you need make sure you
set all references to that pointer to nil.

A slight gotcha is that Lua will only increase the count by 1 regardless
of how many references in Lua you have.  Kinda obvious if you think
about it for a moment, but not so obvious if are thinking about other
things!

You could use a weak table in Lua if you don't want Lua to 'hold on' to
shared pointers, or perhaps you could actually use a boost::weak_ptr ( I
haven't tried this.)  Depends what you are doing.

For instance I use a "ward ptr" for some things, which basically is a
smart pointer that can be marked as invalid so later on if Lua tries to
use it, it throws an exception/Lua error rather than crashing.  Which is
nice. :-P  

HTH

Nigel

On Mon, 2011-06-06 at 22:40 +0100, Simon Pickles wrote:

> Hi,
>
> I am passing boost::shared_ptr<T> to Lua functions, which works well.
>
> Unfortunately, the shared_ptr then keeps an extra use_count (held in the
> lua table), even if the function is finished. How do I release this usage?
>
> Set to nil? Use a Policy? Pass some other sort of pointer? I'd love to
> be able to use the functionality of boost::weak_ptr in lua!
>
> Any advice welcome
>
> thanks
>
> Simon
>
>
>
> ------------------------------------------------------------------------------
> EditLive Enterprise is the world's most technically advanced content
> authoring tool. Experience the power of Track Changes, Inline Image
> Editing and ensure content is compliant with Accessibility Checking.
> http://p.sf.net/sfu/ephox-dev2dev
> _______________________________________________
> luabind-user mailing list
> [hidden email]
> https://lists.sourceforge.net/lists/listinfo/luabind-user



------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Discarding shared_ptrs

Jason McKesson
In reply to this post by Simon Pickles-3
On 6/6/2011 2:40 PM, Simon Pickles wrote:

> Hi,
>
> I am passing boost::shared_ptr<T>  to Lua functions, which works well.
>
> Unfortunately, the shared_ptr then keeps an extra use_count (held in the
> lua table), even if the function is finished. How do I release this usage?
>
> Set to nil? Use a Policy? Pass some other sort of pointer? I'd love to
> be able to use the functionality of boost::weak_ptr in lua!
>
> Any advice welcome
>
> thanks
>
> Simon
>
You didn't state whether Lua was holding on to the pointer after the Lua
function returned or not. If the Lua function stores the
boost::shared_ptr, then that is the correct behavior. Lua now owns a
shared_ptr object, and like any code that owns a shared_ptr object, the
use_count increases.

However, even if the Lua function doesn't store the shared_ptr in
permanent memory (ie: only uses it as an argument or in local variables
of that function), Lua is a garbage-collected environment. Which means
Lua may hold on to the reference to this object for as long as it wants.
If you need deterministic release of references, then you'll have to do
something else.

There's no reason you can't use a weak_ptr in Lua. The weak_ptr is a C++
object, just like any other. You can bind that object with Luabind and
so forth.

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Discarding shared_ptrs

Simon Pickles-3
On 07/06/2011 01:11, Jason McKesson wrote:
> There's no reason you can't use a weak_ptr in Lua. The weak_ptr is a C++
> object, just like any other. You can bind that object with Luabind and
> so forth.
Well I tried using shared_ptrs and at the end of the function, setting
them to nil in Lua but it doesnt decrement the use_count:

-- Lua
-- entity is a boost::shared_ptr<Entity> in c++
-- entity.use_count() in c++ = 3
mission["enter"] = function( entity )
{
   entity:DoSomething()
   entity = nil
}
-- entity.use_count() in c++ = 4

It appears the 'entity = nil' line has no effect.

I'll try weak pointers

S

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Discarding shared_ptrs

Willi Schinmeyer
  On 07.06.2011 09:15:29, Simon Pickles <[hidden email]> wrote:
> Well I tried using shared_ptrs and at the end of the function, setting
> them to nil in Lua but it doesnt decrement the use_count:
Did you force a garbage collection after that? Lua might not immediately
delete an unreferenced object, if I remember correctly.

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user
Reply | Threaded
Open this post in threaded view
|

Re: Discarding shared_ptrs

Simon Pickles-3
On 07/06/2011 09:21, Willi wrote:
>
> Did you force a garbage collection after that? Lua might not immediately
> delete an unreferenced object, if I remember correctly.
>
>
Ah I just came back to the list to say I needed to call
collectgarbage(). Thanks anyway willi

Si

------------------------------------------------------------------------------
EditLive Enterprise is the world's most technically advanced content
authoring tool. Experience the power of Track Changes, Inline Image
Editing and ensure content is compliant with Accessibility Checking.
http://p.sf.net/sfu/ephox-dev2dev
_______________________________________________
luabind-user mailing list
[hidden email]
https://lists.sourceforge.net/lists/listinfo/luabind-user