
2010/6/9 Matheus Araújo Aguiar
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