Boost C++ Libraries: Ticket #4960: boost::pool_allocator for vector of vectors exhausts memory https://svn.boost.org/trac10/ticket/4960 <p> glr9940 reported in <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/386" title="#386: Bugs: boost::pool_allocator breaks with vector of vectors (closed: fixed)">#386</a> that using boost::pool_allocator for a vector of vectors caused problems. I have now found a slight extension to the example glr9940 reported which does not crash, but exhausts memory even for a tiny vector of vectors. </p> <p> I have used the development trunk code and gcc 4.1.2. </p> <div class="wiki-code"><div class="code"><pre><span class="cp">#include</span> <span class="cpf">&lt;boost/pool/pool_alloc.hpp&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;vector&gt;</span><span class="cp"></span> <span class="cp">#include</span> <span class="cpf">&lt;iostream&gt;</span><span class="cp"></span> <span class="k">typedef</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="kt">int</span><span class="p">,</span> <span class="n">boost</span><span class="o">::</span><span class="n">pool_allocator</span><span class="o">&lt;</span><span class="kt">int</span><span class="o">&gt;</span> <span class="o">&gt;</span> <span class="n">EventVector</span><span class="p">;</span> <span class="k">typedef</span> <span class="n">std</span><span class="o">::</span><span class="n">vector</span><span class="o">&lt;</span><span class="n">EventVector</span><span class="o">&gt;</span> <span class="n">IndexVector</span><span class="p">;</span> <span class="kt">int</span> <span class="nf">main</span><span class="p">()</span> <span class="p">{</span> <span class="n">IndexVector</span> <span class="n">iv</span><span class="p">;</span> <span class="kt">int</span> <span class="n">limit</span> <span class="o">=</span> <span class="mi">100</span><span class="p">;</span> <span class="k">for</span> <span class="p">(</span><span class="kt">int</span> <span class="n">i</span> <span class="o">=</span> <span class="mi">0</span><span class="p">;</span> <span class="n">i</span> <span class="o">&lt;</span> <span class="n">limit</span><span class="p">;</span> <span class="o">++</span><span class="n">i</span><span class="p">)</span> <span class="n">iv</span><span class="p">.</span><span class="n">push_back</span><span class="p">(</span><span class="n">EventVector</span><span class="p">());</span> <span class="n">std</span><span class="o">::</span><span class="n">cout</span> <span class="o">&lt;&lt;</span> <span class="s">&quot;it works</span><span class="se">\n</span><span class="s">&quot;</span><span class="p">;</span> <span class="k">return</span> <span class="mi">0</span><span class="p">;</span> <span class="p">}</span> </pre></div></div><p> I suspect that the critical value for 'limit' depends on the machine. On my machine a limit of 20 seems to work fine, a limit of 24 uses a few Gb of memory, while a limit of 30 stalls the process, 'top' showing that the memory footprint doubles rapidly until memory is exhausted. I've noticed for other examples of this issue that when I replace the vector push_back loop with a rolled out version just creating a number of pooled vectors, it works fine. </p> <p> Have I made some noob mistake, or is this a bug? </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/4960 Trac 1.4.3 Steven Watanabe Fri, 17 Dec 2010 17:51:13 GMT <link>https://svn.boost.org/trac10/ticket/4960#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:1</guid> <description> <p> I can't reproduce the problem with gcc 4.4.1, or with MSVC 10. I'd guess that it has something to do with the standard library implementation of vector, though. </p> <p> Can you try applying the patch to singleton_pool which I'm about to attach, run the following variation and save the output? It should produce code that you can compile which demonstrates the problem. Check that it actually triggers the problem, and attach it. </p> <pre class="wiki">#include &lt;boost/pool/pool_alloc.hpp&gt; #include &lt;vector&gt; #include &lt;iostream&gt; typedef std::vector&lt;int, boost::pool_allocator&lt;int&gt; &gt; EventVector; typedef std::vector&lt;EventVector&gt; IndexVector; int main() { std::cout &lt;&lt; "#include &lt;boost/pool/pool_alloc.hpp&gt;\n"; std::cout &lt;&lt; "int main() {\n"; std::cout &lt;&lt; "void* tmp;\n"; { IndexVector iv; int limit = 30; for (int i = 0; i &lt; limit; ++i) iv.push_back(EventVector()); } std::cout &lt;&lt; "}\n"; return 0; } </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Fri, 17 Dec 2010 17:52:19 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4960 https://svn.boost.org/trac10/ticket/4960 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">singleton_pool.hpp.patch</span> </li> </ul> <p> Patch adding instrumentation to singleton_pool </p> Ticket mattiasg Tue, 21 Dec 2010 13:40:15 GMT attachment set https://svn.boost.org/trac10/ticket/4960 https://svn.boost.org/trac10/ticket/4960 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">poolalloctest2_rhel5</span> </li> </ul> <p> Output of the modified test program with the patched singleton_pool.hpp </p> Ticket mattiasg Tue, 21 Dec 2010 13:44:08 GMT <link>https://svn.boost.org/trac10/ticket/4960#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:2</guid> <description> <p> On a Suse 11 machine with gcc 4.3.4 I cannot reproduce the problem, but on a RHEL5 machine with gcc 4.1.2 a run with your patched singleton_pool.hpp produces the output which I've attached to this issue. To avoid crashing the server I ran the test on I had to abort the program around the time it used half (~16Gb) of the available memory. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Steven Watanabe</dc:creator> <pubDate>Sun, 02 Jan 2011 03:19:57 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4960#comment:3 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:3</guid> <description> <p> Sorry for the delay. The file you attached is an executable. I don't think it's the output. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>mattiasg</dc:creator> <pubDate>Tue, 04 Jan 2011 13:32:35 GMT</pubDate> <title>attachment set https://svn.boost.org/trac10/ticket/4960 https://svn.boost.org/trac10/ticket/4960 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">rhel5_out2</span> </li> </ul> <p> Proper output file </p> Ticket mattiasg Tue, 04 Jan 2011 13:33:38 GMT <link>https://svn.boost.org/trac10/ticket/4960#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:4</guid> <description> <p> Ahhh, crap. Attached the output file now, sorry. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Fri, 07 Jan 2011 11:27:04 GMT</pubDate> <title>cc set https://svn.boost.org/trac10/ticket/4960#comment:5 https://svn.boost.org/trac10/ticket/4960#comment:5 <ul> <li><strong>cc</strong> <span class="trac-author">boostpool@…</span> added </li> </ul> Ticket John Maddock Mon, 14 Feb 2011 18:44:04 GMT <link>https://svn.boost.org/trac10/ticket/4960#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:6</guid> <description> <p> I can reproduce this with gcc-4.0.4 as well. Strange bug! </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Tue, 15 Feb 2011 18:34:04 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4960#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:7</guid> <description> <p> Update: </p> <p> This is a result of two bugs: </p> <p> 1) std::vector invokes undefined behaviour by calling Allocator::allocate(0). This has been fixed in more recent GCC versions. 2) Boost.Pool, when it tries to allocate 0 chunks thinks allocation has failed (a bug), so allocates a whole new block, *and* ups the size of the next block to allocate by a factor of 2. So just a few allocation requests for 0 chunks blows up the heap. </p> <p> This will be fixed in the sandbox version of Boost.Pool soon. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Tue, 15 Feb 2011 18:39:10 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/4960#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/4960#comment:8</guid> <description> <p> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/68920" title="Add test case for bug #4960. Add conditional instrumentation code to ...">[68920]</a>) Add test case for bug <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4960" title="#4960: Bugs: boost::pool_allocator for vector of vectors exhausts memory (closed: fixed)">#4960</a>. Add conditional instrumentation code to headers so we can see what's being allocated when. Fix issue <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4960" title="#4960: Bugs: boost::pool_allocator for vector of vectors exhausts memory (closed: fixed)">#4960</a> by allowing pool to allocate 0 chunks. Refs <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4960" title="#4960: Bugs: boost::pool_allocator for vector of vectors exhausts memory (closed: fixed)">#4960</a>. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>John Maddock</dc:creator> <pubDate>Tue, 02 Aug 2011 17:04:12 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/4960#comment:9 https://svn.boost.org/trac10/ticket/4960#comment:9 <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/73495" title="Merge updated Pool lib from trunk. Fixes #1252. Fixes #2696. Fixes ...">[73495]</a>) Merge updated Pool lib from trunk. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/1252" title="#1252: Bugs: [pool] severe overhead with unaligned sizes. (closed: fixed)">#1252</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2696" title="#2696: Bugs: max_size for boost pool (closed: fixed)">#2696</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/4960" title="#4960: Bugs: boost::pool_allocator for vector of vectors exhausts memory (closed: fixed)">#4960</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5526" title="#5526: Bugs: fast_pool_allocator crash (closed: fixed)">#5526</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5568" title="#5568: Bugs: singleton_pool doesn't propagate MaxSize value to his pool (closed: fixed)">#5568</a>. Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5700" title="#5700: Bugs: warning in object_pool.hpp:56 (closed: fixed)">#5700</a>. </p> Ticket