Boost C++ Libraries: Ticket #4089: Memory leak in Boost.Signals2 when the signal is not invoked https://svn.boost.org/trac10/ticket/4089 <p> The attached program leaks memory by attaching and detaching slots from a signal in a loop: </p> <pre class="wiki">boost::signals2::signal&lt;void(void)&gt; sig; boost::signals2::connection conn1, conn2, conn3, conn4; while (true) { conn1.disconnect(); conn2.disconnect(); conn1 = sig.connect(slot); conn2 = sig.connect(slot); conn3.disconnect(); conn4.disconnect(); conn3 = sig.connect(slot); conn4 = sig.connect(slot); //sig(); } </pre><p> Invoking the signal (i.e. uncommenting the 'sig()' line) plugs the memory leak, however in my original setting it entails lots of unnecessary work so it's not a very good solution. </p> <p> My guess is that _garbage_collector_it is set to _shared_state-&gt;connection_bodies().end() on invocation, but not on a new connection, so it never goes back to the beginning of the list. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4089 Trac 1.4.3 dan.berindei@… Mon, 12 Apr 2010 16:57:17 GMT attachment set https://svn.boost.org/trac10/ticket/4089 https://svn.boost.org/trac10/ticket/4089 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">aaa.cpp</span> </li> </ul> <p> Leak Test </p> Ticket Frank Mori Hess Tue, 13 Apr 2010 13:02:19 GMT <link>https://svn.boost.org/trac10/ticket/4089#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4089#comment:1</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/61243" title="Fix for uncontrolled growth of slot list under certain patterns of ...">[61243]</a>) Fix for uncontrolled growth of slot list under certain patterns of connection/disconnection without invocation. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4089" title="#4089: Bugs: Memory leak in Boost.Signals2 when the signal is not invoked (closed: invalid)">#4089</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Frank Mori Hess</dc:creator> <pubDate>Wed, 14 Apr 2010 20:30:39 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4089#comment:2 https://svn.boost.org/trac10/ticket/4089#comment:2 <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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/61278" title="Merged from trunk to release. Fixes #4089. ">[61278]</a>) Merged from trunk to release. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4089" title="#4089: Bugs: Memory leak in Boost.Signals2 when the signal is not invoked (closed: invalid)">#4089</a>. </p> Ticket julien.puydt@… Fri, 27 Jun 2014 14:48:45 GMT status, version, milestone changed; resolution deleted https://svn.boost.org/trac10/ticket/4089#comment:3 https://svn.boost.org/trac10/ticket/4089#comment:3 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>version</strong> <span class="trac-field-old">Boost 1.42.0</span> → <span class="trac-field-new">Boost 1.55.0</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">fixed</span> </li> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.43.0</span> → <span class="trac-field-new">To Be Determined</span> </li> </ul> <p> The following code gives wrong results with 1.55.0 ; it shows kept references trough a disconnection, which get released only if the signal is triggered : </p> <pre class="wiki">#include &lt;boost/smart_ptr.hpp&gt; #include &lt;boost/signals2.hpp&gt; #include &lt;iostream&gt; void print (boost::shared_ptr&lt;int&gt; &amp; ptr) { std::cout &lt;&lt; "Pointer has use count: " &lt;&lt; ptr.use_count () &lt;&lt; std::endl; } int main (int argc, char* argv[]) { boost::signals2::signal&lt;void(void)&gt; sig; boost::shared_ptr&lt;int&gt; ptr(new int); // one ref here: should be 1 print (ptr); boost::signals2::connection conn = sig.connect (boost::bind (&amp;print, ptr)); // one ref here, one in the bind: should be 2 print (ptr); // is supposed to release the bind reference conn.disconnect (); // one ref here: should be 1 print (ptr); // this isn't supposed to release a reference! sig (); // one ref here: should be 1 print (ptr); return 0; } </pre> Ticket Frank Mori Hess Fri, 27 Jun 2014 18:16:45 GMT <link>https://svn.boost.org/trac10/ticket/4089#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4089#comment:4</guid> <description> <p> The behavior described by the most recent comment is by design and not a memory leak. There is already a ticket (<a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8533" title="#8533: Tasks: signal garbage collection of slot objects (closed: fixed)">#8533</a>) to change the designed behavior to what you were expecting. </p> </description> <category>Ticket</category> </item> <item> <author>julien.puydt@…</author> <pubDate>Fri, 27 Jun 2014 19:12:47 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4089#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4089#comment:5</guid> <description> <p> It's what I was expecting because that's what I used to have. Though I can't tell if it was with signals v1 or v2... </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Frank Mori Hess</dc:creator> <pubDate>Fri, 15 Aug 2014 21:32:46 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4089#comment:6 https://svn.boost.org/trac10/ticket/4089#comment:6 <ul> <li><strong>status</strong> <span class="trac-field-old">reopened</span> → <span class="trac-field-new">closed</span> </li> <li><strong>resolution</strong> → <span class="trac-field-new">invalid</span> </li> </ul> Ticket