|
Boost Users : |
Subject: Re: [Boost-users] shared_ptr + bind, is it ok?
From: Emil Dotchevski (emil_at_[hidden])
Date: 2010-06-09 22:43:44
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.
Emil Dotchevski
Reverge Studios, Inc.
http://www.revergestudios.com/reblog/index.php?n=ReCode
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