Boost C++ Libraries: Ticket #13500: Memory leak when using erase on string vectors https://svn.boost.org/trac10/ticket/13500 <p> Consider the following example: </p> <pre class="wiki"> #include &lt;boost/container/vector.hpp&gt; #include &lt;boost/container/string.hpp&gt; int main() { boost::container::vector&lt;boost::container::string&gt; myVec; // myVec.push_back("Short string"); myVec.push_back("This is a long string that exceeds a certain threshold (23 in my case)"); myVec.erase(myVec.begin()); return 0; } </pre><p> When running this example and pushing back the long string, valgrind reports a memory leak (see attached file). When using the short string, there is no leak. I assume this has to do with the boost short string optimization, which puts shorter strings into a buffer living in stack instead of allocating a buffer on the heap. </p> <p> Also, when I do not erase(..) the element but let the vector destructor clean up the memory, there is no leak. </p> <p> Looking at the implementation of vector's erase(..): </p> <pre class="wiki"> //! &lt;b&gt;Effects&lt;/b&gt;: Erases the element at position pos. //! //! &lt;b&gt;Throws&lt;/b&gt;: Nothing. //! //! &lt;b&gt;Complexity&lt;/b&gt;: Linear to the elements between pos and the //! last element. Constant if pos is the last element. iterator erase(const_iterator position) { BOOST_ASSERT(this-&gt;priv_in_range(position)); const pointer p = vector_iterator_get_ptr(position); T *const pos_ptr = boost::movelib::to_raw_pointer(p); T *const beg_ptr = this-&gt;priv_raw_begin(); T *const new_end_ptr = ::boost::container::move(pos_ptr + 1, beg_ptr + this-&gt;m_holder.m_size, pos_ptr); //Move elements forward and destroy last this-&gt;priv_destroy_last(pos_ptr == new_end_ptr); return iterator(p); } </pre><p> I think the condition passed as argument to priv_destroy_last(const bool moved) has to be negated? pos_ptr == new_end_ptr is fulfilled if ::boost::container::move has NOT moved anything. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/13500 Trac 1.4.3 pleemann@… Thu, 29 Mar 2018 11:26:24 GMT attachment set https://svn.boost.org/trac10/ticket/13500 https://svn.boost.org/trac10/ticket/13500 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">boost_leak_valgrind.txt</span> </li> </ul> <p> valgrind output </p> Ticket Ion Gaztañaga Tue, 03 Apr 2018 22:41:04 GMT <link>https://svn.boost.org/trac10/ticket/13500#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/13500#comment:1</guid> <description> <p> Many thanks for the report and the explanations. Fixed in: </p> <p> <a class="ext-link" href="https://github.com/boostorg/container/commit/b3eee90a81ce935918ae1c3c764ae166a8d347df"><span class="icon">​</span>https://github.com/boostorg/container/commit/b3eee90a81ce935918ae1c3c764ae166a8d347df</a> </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Tue, 03 Apr 2018 22:41:11 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/13500#comment:2 https://svn.boost.org/trac10/ticket/13500#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> Ticket