Boost C++ Libraries: Ticket #8977: Memory leak in boost::heap::fibonacci_heap when used with boost::shared_ptr and default allocator https://svn.boost.org/trac10/ticket/8977 <p> There is a bad possible memory leak in the fibonacci_heap when it is used with the default allocator and boost::shared_ptr. </p> <p> Example code: </p> <pre class="wiki">#include &lt;boost/heap/fibonacci_heap.hpp&gt; #include &lt;boost/shared_ptr.hpp&gt; int main(int argc, char* argv[]) { boost::heap::fibonacci_heap&lt;boost::shared_ptr&lt;int&gt; &gt; heap; heap.push(boost::shared_ptr&lt;int&gt;(new int(0))); return 0; } </pre><p> Valgrind says: </p> <pre class="wiki">==8228== HEAP SUMMARY: ==8228== in use at exit: 28 bytes in 2 blocks ==8228== total heap usage: 650 allocs, 648 frees, 105,755 bytes allocated ==8228== ==8228== 28 (24 direct, 4 indirect) bytes in 1 blocks are definitely lost in loss record 2 of 2 ==8228== at 0x4C2BCA7: operator new(unsigned long) (in /usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so) ==8228== by 0x4080A3: boost::detail::shared_count::shared_count&lt;int&gt;(int*) (shared_count.hpp:91) ==8228== by 0x407E05: boost::shared_ptr&lt;int&gt;::shared_ptr&lt;int&gt;(int*) (shared_ptr.hpp:183) ==8228== by 0x40794D: main (SandBox.cpp:99) ==8228== ==8228== LEAK SUMMARY: ==8228== definitely lost: 24 bytes in 1 blocks ==8228== indirectly lost: 4 bytes in 1 blocks ==8228== possibly lost: 0 bytes in 0 blocks ==8228== still reachable: 0 bytes in 0 blocks ==8228== suppressed: 0 bytes in 0 blocks ==8228== ==8228== For counts of detected and suppressed errors, rerun with: -v ==8228== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2) </pre><p> The problem is the line </p> <pre class="wiki">alloc_.deallocate(n, 1); </pre><p> in the struct <em>node_disposer</em> in <em>heap_node.hpp</em>. </p> <p> It does what it says: If it is a std::allocator which the boost::heap::allocator&lt;&gt; defaults to if not specified, it deallocates the memory but it does not call destructors as internally <em>::operator delete()</em> is called on the heap_node. That means that every object that has been pushed into the heap does not get its destructor called. </p> <p> Obviously this is really bad when using a boost::shared_ptr because the use_count will not go to 0. Using a boost::weak_ptr is not a remedy as the boost::shared_ptr's shared_count object that is on the heap will not get deleted as the boost::weak_ptr's destructor is not called and thus the boost::shared_ptr's weak_count does not go to 0. </p> <p> I did not check whether this problem affects the other heaps as well. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8977 Trac 1.4.3 timblechmann Fri, 09 Aug 2013 09:18:28 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/8977#comment:1 https://svn.boost.org/trac10/ticket/8977#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">fixed</span> </li> </ul> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/85250" title="heap: fix memory leak in destructor fixes #8977">[85250]</a>) heap: fix memory leak in destructor </p> <p> fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/8977" title="#8977: Bugs: Memory leak in boost::heap::fibonacci_heap when used with ... (closed: fixed)">#8977</a> </p> Ticket Joel Young <jdy@…> Fri, 03 Jan 2014 21:29:38 GMT <link>https://svn.boost.org/trac10/ticket/8977#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8977#comment:2</guid> <description> <p> When is this fix going to be committed? It isn't checked in to git: git clone --recursive <a class="ext-link" href="https://github.com/boostorg/boost.git"><span class="icon">​</span>https://github.com/boostorg/boost.git</a> </p> </description> <category>Ticket</category> </item> </channel> </rss>