Boost C++ Libraries: Ticket #5582: There is a memory leak in the BOOST_AUTO_TEST_CASE_TEMPLATE https://svn.boost.org/trac10/ticket/5582 <p> There is memory leak in the boost::unit_test::ut_detail::generate_test_case_4_type::operator() which is used by boost::unit_test::ut_detail::template_test_case_gen which in its turn is used by BOOST_AUTO_TEST_CASE_TEMPLATE </p> <p> The problematic code is provided below. The test_case created here never gets deleted. </p> <pre class="wiki">template&lt;typename TestType&gt; void operator()( mpl::identity&lt;TestType&gt; ) { ... m_holder.m_test_cases.push_back( new test_case( full_name, test_case_template_invoker&lt;TestCaseTemplate,TestType&gt;() ) ); } </pre><p> Below is the code that can be used to reproduce the issue: </p> <pre class="wiki">#define BOOST_TEST_MAIN #include &lt;boost/test/auto_unit_test.hpp&gt; #include &lt;boost/test/test_tools.hpp&gt; #include &lt;boost/test/test_case_template.hpp&gt; #include &lt;boost/mpl/list.hpp&gt; BOOST_AUTO_TEST_SUITE( TestMemLeak ) BOOST_AUTO_TEST_CASE_TEMPLATE( TestCase, Type, boost::mpl::list&lt;int&gt; ) { } BOOST_AUTO_TEST_SUITE_END() </pre><p> <strong>Note:</strong> This leak is detected only when application is linked with C runtime statically. Configured test application can be found in the attachment </p> <p> Execution output: </p> <pre class="wiki">Running 1 test case... *** No errors detected Detected memory leaks! Dumping objects -&gt; {103} normal block at 0x00808D50, 4 bytes long. Data: &lt;int &gt; 69 6E 74 00 {102} normal block at 0x00808D18, 8 bytes long. Data: &lt;P &gt; 50 8D 80 00 00 00 00 00 Object dump complete. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/5582 Trac 1.4.3 Anton Matosov <anton.matosov@…> Mon, 30 May 2011 15:09:04 GMT attachment set https://svn.boost.org/trac10/ticket/5582 https://svn.boost.org/trac10/ticket/5582 <ul> <li><strong>attachment</strong> → <span class="trac-field-new">BoostTestMemoryLeak.zip</span> </li> </ul> <p> Configured VS2008 test application which reproduces the memory leak in boost::test library </p> Ticket harningt@… Mon, 25 Jul 2011 15:47:23 GMT <link>https://svn.boost.org/trac10/ticket/5582#comment:1 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5582#comment:1</guid> <description> <p> Any status change on this? If there is a workaround that I can put in place to alleviate this problem before it is put in a release, that would be great. </p> </description> <category>Ticket</category> </item> <item> <author>Anton Matosov <anton.matosov@…></author> <pubDate>Tue, 16 Aug 2011 07:55:53 GMT</pubDate> <title/> <link>https://svn.boost.org/trac10/ticket/5582#comment:2 </link> <guid isPermaLink="false">https://svn.boost.org/trac10/ticket/5582#comment:2</guid> <description> <p> It seems that issue is not in boost test library, but with memory leaks detection itself. The leak detected is a memory allocated for <em>type_info</em> (<strong>typeid</strong>) but not yet freed. </p> <p> Here is the MSDN page related to the issue: <a class="ext-link" href="http://connect.microsoft.com/VisualStudio/feedback/details/106937/memory-leaks-reported-by-debug-crt-inside-typeinfo-name"><span class="icon">​</span>http://connect.microsoft.com/VisualStudio/feedback/details/106937/memory-leaks-reported-by-debug-crt-inside-typeinfo-name</a> </p> <p> Here is the smallest code that reproduces the "leak": </p> <pre class="wiki">#include &lt;crtdbg.h&gt; #include &lt;typeinfo&gt; // for typeid int main() { _CrtSetReportMode(_CRT_WARN, _CRTDBG_MODE_FILE); _CrtSetReportFile(_CRT_WARN, _CRTDBG_FILE_STDOUT); _CrtSetDbgFlag(_CrtSetDbgFlag( _CRTDBG_REPORT_FLAG ) | _CRTDBG_LEAK_CHECK_DF); typeid(int).name(); return 0; } </pre><p> The good news here is that there is a workaround for this issue provided on the MSDN page specified above. Minimum code that worked in my case is: </p> <pre class="wiki">void free_typeinfo_memory() { __type_info_node *pNode = __type_info_root_node.next; __type_info_node *tmpNode = &amp;__type_info_root_node; for( ; pNode!=NULL; pNode = tmpNode ) { tmpNode = pNode-&gt;next; delete pNode-&gt;memPtr; delete pNode; } } </pre><p> I have placed the call to <strong>free_typeinfo_memory()</strong> at the very last line of the <strong>main()</strong> and leaks didn't come up again :) </p> </description> <category>Ticket</category> </item> <item> <dc:creator>Gennadiy Rozental</dc:creator> <pubDate>Sun, 16 Oct 2011 05:12:03 GMT</pubDate> <title>status changed; resolution set https://svn.boost.org/trac10/ticket/5582#comment:3 https://svn.boost.org/trac10/ticket/5582#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> (In <a class="changeset" href="https://svn.boost.org/trac10/changeset/74958" title="Introduces notion of framework shutdown Fixes #5582">[74958]</a>) Introduces notion of framework shutdown Fixes <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/5582" title="#5582: Bugs: There is a memory leak in the BOOST_AUTO_TEST_CASE_TEMPLATE (closed: fixed)">#5582</a> </p> Ticket Raffi Enficiaud Tue, 07 Jul 2015 08:23:45 GMT milestone changed https://svn.boost.org/trac10/ticket/5582#comment:4 https://svn.boost.org/trac10/ticket/5582#comment:4 <ul> <li><strong>milestone</strong> <span class="trac-field-old">Boost 1.47.0</span> → <span class="trac-field-new">Boost 1.59.0</span> </li> </ul> Ticket