Boost logo

Boost Users :

Subject: Re: [Boost-users] shared_ptr + bind, is it ok?
From: Matheus Araújo Aguiar (matheus.pit_at_[hidden])
Date: 2010-06-10 10:45:42


On Wed, Jun 9, 2010 at 11:43 PM, Emil Dotchevski <emil_at_[hidden]>wrote:

> 2010/6/9 Matheus Araújo Aguiar <matheus.pit_at_[hidden]>:
> > Hello list,
> > In my application i am using a shared_ptr to manage the lifetime of some
> > important objects. Lately i've been noticing that they are not being
> > destroyed (their destructor is never called) and their use count is 9.
> > I've started debugging and noticed that if i pass the shared_ptr to
> > boost::bind as a parameter when the binded function is executed the
> > use_count is greater than what would be expected (in my particular case 4
> > instead of one). As i make use of boost::bind, the shared_ptr reaches a
> > point where its use_count is way out of expectations, getting to 9.
> > So is this expected? Am i incorrectly using boost::bind or shared_ptr in
> > this situation?
>
> By design, boost::bind makes copies of all of its arguments and stores
> them into the returned function object. If any of these arguments is a
> shared_ptr instance, that will increase the refcount. If that's not
> desirable, you can instead pass sp.get() to bind. You can also wrap
> the arguments using boost::ref/cref if you want bind to store
> references instead of copies.
>
Thanks Emil.

Today I have tracked down the bug in a third-part library i was using: it
was never releasing the function objects holding the shared_ptr's.

 Best Regards,

>
> Emil Dotchevski
> Reverge Studios, Inc.
> http://www.revergestudios.com/reblog/index.php?n=ReCode
> _______________________________________________
> Boost-users mailing list
> Boost-users_at_[hidden]
> http://lists.boost.org/mailman/listinfo.cgi/boost-users
>

-- 
Matheus Araújo Aguiar
Computer Scientist
matheus.pit_at_[hidden]


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net