Boost C++ Libraries: Ticket #7724: executing signals2::signal::connect creates many copies of the connected object https://svn.boost.org/trac10/ticket/7724 <p> I am trying to figure out where is the problem, maybe what I have implemented is incorrect or it is a bug. </p> <p> When trying to connect to a signal the passed object is copied many times and the final triggered signal executes a method on a copy of the passed object. </p> <p> Signal is the following type: </p> <pre class="wiki">typedef signals2::signal&lt;void (NotificationCenter::Notification)&gt; Signal; </pre><p> Following code (<a class="missing wiki">NotificationCenter</a> creates a shared_ptr&lt;&gt; to the requested signals): </p> <pre class="wiki">class NotificationReceived { public: int runCount; NotificationReceived() { std::cout &lt;&lt; "* Receiver CREATED" &lt;&lt; std::endl; runCount = 0; } NotificationReceived(const NotificationReceived &amp;cSource) { std::cout &lt;&lt; "* Receiver COPIED" &lt;&lt; std::endl; runCount = cSource.runCount; } ~NotificationReceived() { std::cout &lt;&lt; "* Receiver DELETED" &lt;&lt; std::endl; } void Receive(NotificationCenter::Notification notif) { std::cout &lt;&lt; std::endl &lt;&lt; "Receive: Notification received: " + notif.m_information &lt;&lt; std::endl &lt;&lt; std::endl; runCount++; } }; BOOST_AUTO_TEST_CASE( test_notification_center_notify) { NotificationCenter nCenter(NotificationCenter::NCT_DELAYED); NotificationReceived receiver; NotificationCenter::Signal* sig = nCenter.RegisterForNotification(NotificationCenter::Notification::NT_BROADCAST); sig-&gt;connect(boost::bind(&amp;NotificationReceived::Receive, receiver, _1)); nCenter.BroadcastNotification(NotificationCenter::Notification(NotificationCenter::Notification::NT_BROADCAST, "Testing")); boost::this_thread::sleep(boost::posix_time::seconds(2)); BOOST_REQUIRE_EQUAL(receiver.runCount, 1); } </pre><p> Has following output: </p> <pre class="wiki">* Receiver CREATED - Queue empty - sleeping 500ms * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver DELETED * Receiver DELETED * Receiver DELETED * Receiver COPIED * Receiver DELETED * Receiver DELETED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver COPIED * Receiver DELETED * Receiver DELETED * Receiver DELETED * Receiver DELETED * Receiver DELETED * Receiver DELETED * Receiver COPIED * Receiver DELETED * Receiver COPIED * Receiver DELETED * Receiver DELETED * Receiver COPIED * Receiver DELETED * Receiver DELETED - Thread waked up - Queue not empty, searching receivers ... - Sending notification Receive: Notification received: Testing - Queue empty - sleeping 500ms - Thread waked up - Queue empty - sleeping 500ms - Thread waked up - Queue empty - sleeping 500ms * Receiver DELETED * Receiver DELETED *** 1 failure detected in test suite "FrameworkTests" </pre><p> <strong>Why the "receiver" object has been copied many times and the final method has not been executed on the original object but on a copy which proves the check of the "runCount"?</strong> </p> <p> I have managed to go around this problem adding a shared_ptr&lt;&gt; </p> <p> Following code (classes the same): </p> <pre class="wiki"> NotificationCenter nCenter(NotificationCenter::NCT_DELAYED); boost::shared_ptr&lt;NotificationReceived&gt; receiver(new NotificationReceived); NotificationCenter::Signal* sig = nCenter.RegisterForNotification(NotificationCenter::Notification::NT_BROADCAST); sig-&gt;connect(boost::bind(&amp;NotificationReceived::Receive, receiver, _1)); nCenter.BroadcastNotification(NotificationCenter::Notification(NotificationCenter::Notification::NT_BROADCAST, "Testing")); boost::this_thread::sleep(boost::posix_time::seconds(2)); BOOST_REQUIRE_EQUAL(receiver-&gt;runCount, 1); </pre><p> Outputs what I expected also from the first example: </p> <pre class="wiki">* Receiver CREATED - Queue empty - sleeping 500ms - Thread waked up - Queue not empty, searching receivers ... - Sending notification Receive: Notification received: Testing - Queue empty - sleeping 500ms - Thread waked up - Queue empty - sleeping 500ms - Thread waked up - Queue empty - sleeping 500ms * Receiver DELETED *** No errors detected </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/7724 Trac 1.4.3 Frank Mori Hess Thu, 22 Nov 2012 15:40:43 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/7724#comment:1 https://svn.boost.org/trac10/ticket/7724#comment:1 <ul> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> <p> It's not a bug. Slots are copied by value, by design. If you want, you can submit a separate bug regarding the poor documentation of this fact. </p> Ticket