Problem with signals2 and shared_ptr

Hi All, I am using boost 1.41.0 on Windows with Visual Studio 2008 and am seeing what seems to be an issue with signals2 and shared_ptr. The problem is that the shared_ptr does not decrement is reference count after I disconnect the signals2::connection. This is probably best illustrated with some code. ===== Test.h===== #ifndef _CLASS1_H #define _CLASS1_H #include <boost/signals2/signal.hpp> #include <boost/smart_ptr.hpp> class Callee : public boost::enable_shared_from_this<Callee> { public: Callee() : mInt(0) {}; virtual ~Callee() {}; void func1() { mInt++; }; private: int mInt; }; class Caller { public: typedef boost::signals2::signal<void (boost::shared_ptr<Callee>)> OnRun; public: Caller() : mOnRun() {}; ~Caller() {}; void run(boost::shared_ptr<Callee> sp) { boost::signals2::connection conn = mOnRun.connect(boost::bind(&Callee::func1, sp)); _sleep(1000); conn.disconnect(); }; private: OnRun mOnRun; }; #endif ===== End Test.h===== ======main.cpp====== int main(int argc, char **argv) { Caller caller; { boost::shared_ptr<Callee> sp(new Callee); caller.run(sp); } } ======End main.cpp====== Running this in the debugger I would expect to hit a breakpoint in the Callee class before the Caller class, this is not the case I destruct Caller before Callee. Having dug in a bit I see the use_count of the shared pointer go to 2 while in the caller.run which I would expect. The strange thing is the use_count does not decrement after the disconnect or exiting the caller.run function. Is there a way to force the connection/signal to release that shared_ptr? Thanks Glenn

Glenn Macgregor wrote:
I am using boost 1.41.0 on Windows with Visual Studio 2008 and am seeing what seems to be an issue with signals2 and shared_ptr. The problem is that the shared_ptr does not decrement is reference count after I disconnect the signals2::connection.
<snip>
The strange thing is the use_count does not decrement after the disconnect or exiting the caller.run function.
Is there a way to force the connection/signal to release that shared_ptr?
This thread may discuss the same issue: http://old.nabble.com/Re%3A--signals2--review--The-review-of-the-signals2-li... Frank has stated that cleaning up the slot earlier is actually a tricky problem, because of the necessity to avoid possible race conditions. Nonetheless I too would be interested in a fix that allows a disconnected connection involving a bound shared_ptr to destroy the shared_ptr instance.
participants (2)
-
Glenn Macgregor
-
Nat Goodspeed