Boost C++ Libraries: Ticket #10651: boost::thread leaks memory when using the MinGW compiler https://svn.boost.org/trac10/ticket/10651 <p> boost::thread leaks memory when using the MinGW compiler </p> <p> This report is based on information originally from: </p> <blockquote> <p> [hxxp://stackoverflow.com/questions/26284056/threads-hogging-up-memory] </p> </blockquote> <p> When the following program is compiled with MinGW, the memory use of the process continually increases: </p> <pre class="wiki">#include &lt;boost/thread.hpp&gt; void thread_func() { return; } int main() { for (;;) { boost::thread t1(thread_func); boost::thread t2(thread_func); t1.join(); t2.join(); } } </pre><p> Any one of the following changes to the program seem to make the memory leak go away: </p> <ul><li>add a call to the Windows API <code>Sleep(1)</code> to <code>thread_func()</code> </li><li>use only a single thread </li><li>move the call to <code>t1.join()</code> to before the creation of <code>t2</code> </li></ul><p> I'm not sure if this problem is sensitive to the version of MinGW. I used MinGW 4.8.1 from tdm-gcc: </p> <blockquote> <p> [hxxp://tdm-gcc.tdragon.net/] </p> </blockquote> <p> I built a 32-bit program; I'm not sure if a 64-bit program would behave differently. </p> <p> The problem doesn't occur when MSVC 12 (Visual Studio 2013) is used. </p> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/10651 Trac 1.4.3 viboes Sun, 12 Oct 2014 09:38:05 GMT owner, status changed https://svn.boost.org/trac10/ticket/10651#comment:1 https://svn.boost.org/trac10/ticket/10651#comment:1 <ul> <li><strong>owner</strong> changed from <span class="trac-author">Anthony Williams</span> to <span class="trac-author">viboes</span> </li> <li><strong>status</strong> <span class="trac-field-old">new</span> → <span class="trac-field-new">assigned</span> </li> </ul> Ticket viboes Sun, 12 Oct 2014 09:47:49 GMT <link>https://svn.boost.org/trac10/ticket/10651#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:2</guid> <description> <p> Does the program behaves the same if you define BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE? </p> <p> You could also define BOOST_THREAD_VERSION = 4. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sun, 12 Oct 2014 10:00:17 GMT</pubDate> <title>owner, status changed https://svn.boost.org/trac10/ticket/10651#comment:3 https://svn.boost.org/trac10/ticket/10651#comment:3 <ul> <li><strong>owner</strong> changed from <span class="trac-author">viboes</span> to <span class="trac-author">Niall Douglas</span> </li> <li><strong>status</strong> <span class="trac-field-old">assigned</span> → <span class="trac-field-new">new</span> </li> </ul> <p> There is a line in boost/thread/win32/thread_data.hpp that is suspect </p> <pre class="wiki"> typedef boost::intrusive_ptr&lt;detail::thread_data_base&gt; thread_data_ptr; </pre><p> I have no access to a windows platform. Could you try replacing intrusive_ptr by shared_ptr? </p> <p> Niall, please could you try to reproduce and try this patch? </p> Ticket michael.burr@… Mon, 13 Oct 2014 03:29:45 GMT <link>https://svn.boost.org/trac10/ticket/10651#comment:4 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:4</guid> <description> <p> I added the lines </p> <pre class="wiki">#define BOOST_THREAD_PROVIDES_THREAD_DESTRUCTOR_CALLS_TERMINATE_IF_JOINABLE 1 #define BOOST_THREAD_VERSION 4 </pre><p> to <code>boost/config/user.hpp</code> and rebuilt the library. This didn't have an effect on the problem. </p> <p> But to be honest, I'm not 100% sure this is the right way to do what you asked. </p> <p> I have also tried changing the typedef for <code>thread_data_ptr</code> to: </p> <pre class="wiki">typedef boost::shared_ptr&lt;detail::thread_data_base&gt; thread_data_ptr; </pre><p> but that results in several other compiler errors occurring. I haven't had a chance to work through updaing the uses of <code>thread_data_ptr</code> to resolve those errors. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niall Douglas</dc:creator> <pubDate>Mon, 13 Oct 2014 09:29:17 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10651#comment:5 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:5</guid> <description> <p> I wouldn't be surprised if this leak in Mingw is simply accepted as is. Boost.Thread hooks itself into the PE process state change function pointer table by quite literally marking the memory pages as writable and overwriting the relevant entries. The logic which tracks down where to write is somewhat brittle, and almost certainly made the assumption that MSVCRT is the CRT. </p> <p> VS14 ships a whole new MSVCRT thank god, so it may be the same leak may manifest there too as the heuristics may simply be out of date. </p> <p> Can the OP try Mingw-w64 (this being the much improved rewrite of Mingw) first? Mingw32 is currently not compiling with Boost 1.56 by the way, that's a mea culpa but Mingw-w64 works fine. Mingw32 is also frequently subtly broken, whereas Mingw-w64 tends to have much superior QA e.g. right now Mingw32 fails to enumerate directories reliably. </p> <p> Niall </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 25 Oct 2014 15:26:38 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10651#comment:6 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:6</guid> <description> <p> Niall, have you take a look at the intrusive_ptr? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niall Douglas</dc:creator> <pubDate>Sun, 26 Oct 2014 17:11:18 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10651#comment:7 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:7</guid> <description> <p> Not yet. Last weekend and this one my wife was away so I have been baby minding. And the baby dislikes me looking after her, so I am too tired to think in code. This coming weekend should be back to normal i.e. I get my Saturday back. I am aware of the 1.57 deadline. N </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Niall Douglas</dc:creator> <pubDate>Sat, 01 Nov 2014 16:01:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10651#comment:8 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:8</guid> <description> <p> I ran this test case on Boost 1.57 beta 1 on Mingw with GCC 4.8.1 32 bit on Win8.1 x86 both debug and release builds. I see no leak of memory, it's flat as a pancake at 0.4Mb. Overall system memory does not increase either. I left it run for 10 minutes each build where it drew down about 70% CPU, still no change in RAM consumption. </p> <p> It could be my system is a dual core CPU? If this is a timing related leak it may be unreproducible on my machine? Or else the bug has been fixed, or most likely of all this is yet another bug in Mingw which has poor QA. </p> <p> OP: Can you try Mingw-w64 please and see if it replicates there on your hardware? If it does, it's a bug in Boost, if it doesn't it's a bug in Mingw. </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 06 Dec 2014 09:55:56 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/10651#comment:9 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/10651#comment:9</guid> <description> <p> Any news on this issue? </p> </description> <category>Ticket</category> </item> <item> <dc:creator>viboes</dc:creator> <pubDate>Sat, 13 Dec 2014 16:08:06 GMT</pubDate> <title>type changed https://svn.boost.org/trac10/ticket/10651#comment:10 https://svn.boost.org/trac10/ticket/10651#comment:10 <ul> <li><strong>type</strong> <span class="trac-field-old">Bugs</span> → <span class="trac-field-new">Support Requests</span> </li> </ul> <p> Moved to support, until there is more feedback. </p> Ticket viboes Sat, 04 Apr 2015 21:43:43 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/10651#comment:11 https://svn.boost.org/trac10/ticket/10651#comment:11 <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">wontfix</span> </li> </ul> <p> We need tome tester help in order to be able to fix it. Closed until we get it. </p> Ticket