Re: Luabind and boost::shared_ptr<T> use counts

Jason McKesson <[email protected]>
Newsgroups gmane.comp.lang.lua.bind.user
Message-ID <[email protected]>
On 5/31/2010 2:34 PM, Patrick Hartling wrote:
> I am trying to understand how Luabind handles boost::shared_ptr<T>  objects. Specifically, the use count for any boost::shared_ptr<T>  passed to a Lua function is incremented during the function execution but not decremented once the function call completes. This leads to memory leaks if there is an expectation of the use count on these objects eventually reaching zero.
>
> The attached program demonstrates what I am seeing. If the shared pointer-wrapped object is wrapped in boost::ref() before being passed to Luabind (see line 72), then the use count works out correctly. Is this the correct way to use boost::shared_ptr<T>  with Luabind, or is there a preferred practice? In general, I think it will be fine use to use boost::ref() as long as we are careful.
>
> I tested with Luabind 0.8.1, Boost 1.43, and Lua 5.1.4 on Mac OS X 10.6. The same behavior occurs on Linux.
>
>   -Patrick
>
>
> --
> Patrick L. Hartling
> Senior Software Engineer, Priority 5
> http://www.priority5.com/
>    

Lua is a garbage collected system. The reference will not disappear 
until Lua does garbage collection and actually deallocates the memory 
associated with the shared pointer. Since GC is a costly operation, Lua 
does not constantly sweep and delete from memory. Thus there is no 
guarantee that an object will be deleted immediately.

Generally, you shouldn't worry about this. The object will eventually be 
deleted, even if it happens to sit around until the Lua state is destroyed.

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