Boost C++ Libraries: Ticket #9166: Memory leak when copying empty boost::container::vector https://svn.boost.org/trac10/ticket/9166 <p> Consider the following simple program: </p> <pre class="wiki">#include &lt;boost/container/vector.hpp&gt; int main() { while (true) { boost::container::vector&lt;int&gt; vec; boost::container::vector&lt;int&gt; vec2(vec); } } </pre><p> With Boost 1.54.0, this leaks an unbounded amount of memory. The copy contructor of <code>boost::container::vector</code> performs an allocation (which in this case asks for 0 bytes), and sets the capacity of the new vector the size of this allocation (0 elements), and since the capacity is 0, the destructor of vec2 does not deallocate this memory. </p> <p> A simple patch is attached which disables allocation when the requested size is 0. This may not be the optimal solution (for example, there might be code that subsequently reads from <code>m_start</code> and expects it to contain a valid value (I have not yet studied the code sufficiently to know)). Even so, this is a major issue, and so I am reporting it even without a fully tested patch. </p> <p> Evan Wallace </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/9166 Trac 1.4.3 onlyone@… Thu, 26 Sep 2013 14:56:48 GMT attachment set https://svn.boost.org/trac10/ticket/9166 https://svn.boost.org/trac10/ticket/9166 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">container_leak.patch</span> </li> </ul> <p> Patch that disables allocation when space for 0 elements is needed. </p> Ticket anonymous Thu, 26 Sep 2013 15:10:47 GMT summary changed https://svn.boost.org/trac10/ticket/9166#comment:1 https://svn.boost.org/trac10/ticket/9166#comment:1 <ul> <li><strong>summary</strong> <span class="trac-field-old">Memory when copying empty boost::container::vector</span> → <span class="trac-field-new">Memory leak when copying empty boost::container::vector</span> </li> </ul> Ticket Ion Gaztañaga Thu, 26 Sep 2013 18:05:27 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/9166#comment:2 https://svn.boost.org/trac10/ticket/9166#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/85964" title="Default initialization for vector-like containers Complexity ...">[85964]</a>) Default initialization for vector-like containers Complexity guarantees for associative container constructors and ordered input ranges Added benchmark for associative containers Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/9166" title="#9166: Bugs: Memory leak when copying empty boost::container::vector (closed: fixed)">#9166</a> </p> Ticket