Boost C++ Libraries: Ticket #8724: bad_alloc thrown in empty list copy constructor https://svn.boost.org/trac10/ticket/8724 <p> I am running RHEL 5.<br /> </p> <p> Compiler - gcc 4.1.2<br /> </p> <p> following code fragment compiled against boost 1.53.0 throws bad_alloc exception at run time.<br /> </p> <p> Essentially I have an empty ::boost::container::list object allocated in shared memory (segment_manager from boost interprocess). attempt to copy such empty list causes bad_alloc exception </p> <pre class="wiki">#include &lt;boost/interprocess/allocators/allocator.hpp&gt; #include &lt;boost/interprocess/managed_external_buffer.hpp&gt; namespace { typedef ::boost::interprocess::allocator&lt;int, ::boost::interprocess::managed_external_buffer::segment_manager&gt; int_allocator; typedef ::boost::container::list&lt;int, int_allocator&gt; int_list; char g_memory_buf[1024]; } int main() { ::boost::interprocess::managed_external_buffer segment(::boost::interprocess::create_only, g_memory_buf, sizeof(g_memory_buf)); // 1. construct empty list int_list *l1 = segment.construct&lt;int_list&gt;("1")(int_allocator(segment.get_segment_manager())); // 2. use copy constructor segment.construct&lt;int_list&gt;("2")(*l1); return 0; } </pre><p> when same code is compiled against boost 1.51.0 - no exceptions. I wonder if this behavioral change was intentional. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/8724 Trac 1.4.3 anonymous Mon, 24 Jun 2013 18:40:36 GMT keywords set https://svn.boost.org/trac10/ticket/8724#comment:1 https://svn.boost.org/trac10/ticket/8724#comment:1 <ul> <li><strong>keywords</strong> bad_alloc interprocess container added </li> </ul> Ticket anonymous Mon, 24 Jun 2013 18:42:17 GMT <link>https://svn.boost.org/trac10/ticket/8724#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8724#comment:2</guid> <description> <p> apologies, I have missed one include </p> <pre class="wiki">#include &lt;boost/container/list.hpp&gt; </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Mon, 24 Jun 2013 19:03:05 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8724#comment:3 https://svn.boost.org/trac10/ticket/8724#comment:3 <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> Thanks for the report. It's a bug. </p> <p> To get a workaround, modify allocate_many_and_construct() function of boost/container/detail/node_alloc_holder.hpp file so that if "n" is zero, no code is executed. This function should do nothing if "difference_type n" argument is zero (as it's your case, since the source container is empty. </p> Ticket Ion Gaztañaga Mon, 24 Jun 2013 19:04:13 GMT status changed; resolution deleted https://svn.boost.org/trac10/ticket/8724#comment:4 https://svn.boost.org/trac10/ticket/8724#comment:4 <ul> <li><strong>status</strong> <span class="trac-field-old">closed</span> → <span class="trac-field-new">reopened</span> </li> <li><strong>resolution</strong> <span class="trac-field-deleted">fixed</span> </li> </ul> <p> Sorry, I wrongly closed the issue. Reopening. </p> Ticket gleonid@… Wed, 26 Jun 2013 15:57:24 GMT <link>https://svn.boost.org/trac10/ticket/8724#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8724#comment:5</guid> <description> <p> please confirm that following is a correct patch </p> <pre class="wiki">--- boost-1.53.0/boost/container/detail/node_alloc_holder.hpp.orig 2013-06-25 17:14:06.000000000 -0400 +++ boost-1.53.0/boost/container/detail/node_alloc_holder.hpp 2013-06-25 17:14:37.000000000 -0400 @@ -239,6 +239,8 @@ ::new(static_cast&lt;hook_type*&gt;(container_detail::to_raw_pointer(p))) hook_type; return (p); */ + if (!n) + return; typedef typename NodeAlloc::multiallocation_chain multiallocation_chain; //Try to allocate memory in a single block </pre> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Wed, 26 Jun 2013 19:44:37 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/8724#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/8724#comment:6</guid> <description> <p> Yes, that patch should fix the issue. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Ion Gaztañaga</dc:creator> <pubDate>Thu, 01 Aug 2013 14:06:21 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/8724#comment:7 https://svn.boost.org/trac10/ticket/8724#comment:7 <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">fixed</span> </li> </ul> <p> It was fixed in Boost 1.54 </p> Ticket