Re: Discarding shared_ptrs

Jason McKesson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
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
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.