Boost C++ Libraries: Ticket #3635: basic unit test sample memory issues https://svn.boost.org/trac10/ticket/3635 <p> it seems that the basic unit test sample in the unit test framework documentation has some memory issues. the sample is example01 from page <a href="http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/test-organization/manual-nullary-test-case.html">http://www.boost.org/doc/libs/1_40_0/libs/test/doc/html/utf/user-guide/test-organization/manual-nullary-test-case.html</a> , and it seems to make an invalid read according to valgrind, and leak according to google perftools. </p> <p> output of valgrind: </p> <pre class="wiki">$ cat example01.cpp #include &lt;boost/test/included/unit_test.hpp&gt; using namespace boost::unit_test; //____________________________________________________________________________// void free_test_function() { BOOST_CHECK( true /* test assertion */ ); } //____________________________________________________________________________// test_suite* init_unit_test_suite( int argc, char* argv[] ) { framework::master_test_suite(). add( BOOST_TEST_CASE( &amp;free_test_function ) ); return 0; } //____________________________________________________________________________// $ g++ -o example01 example01.cpp $ $ valgrind ./example01 ==16385== Memcheck, a memory error detector. ==16385== Copyright (C) 2002-2007, and GNU GPL'd, by Julian Seward et al. ==16385== Using LibVEX rev 1854, a library for dynamic binary translation. ==16385== Copyright (C) 2004-2007, and GNU GPL'd, by OpenWorks LLP. ==16385== Using valgrind-3.3.1-Debian, a dynamic binary instrumentation framework. ==16385== Copyright (C) 2000-2007, and GNU GPL'd, by Julian Seward et al. ==16385== For more details, rerun with: -v ==16385== Running 1 test case... *** No errors detected ==16385== Invalid read of size 8 ==16385== at 0x472F35: boost::unit_test::framework_impl::clear() (in /home/maroy/tmp/tests/example01) ==16385== by 0x47308E: boost::unit_test::framework_impl::~framework_impl() (in /home/maroy/tmp/tests/example01) ==16385== by 0x560B6AC: exit (in /lib/libc-2.8.90.so) ==16385== by 0x55F346C: (below main) (in /lib/libc-2.8.90.so) ==16385== Address 0x59516d8 is 40 bytes inside a block of size 48 free'd ==16385== at 0x4C24DAD: operator delete(void*) (vg_replace_malloc.c:342) ==16385== by 0x44EBA4: __gnu_cxx::new_allocator&lt;std::_Rb_tree_node&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::deallocate(std::_Rb_tree_node&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;*, unsigned long) (in /home/maroy/tmp/tests/example01) ==16385== by 0x44EBCC: std::_Rb_tree&lt;unsigned long, std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt;, std::_Select1st&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::_M_put_node(std::_Rb_tree_node&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;*) (in /home/maroy/tmp/tests/example01) ==16385== by 0x44EC17: std::_Rb_tree&lt;unsigned long, std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt;, std::_Select1st&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::_M_destroy_node(std::_Rb_tree_node&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;*) (in /home/maroy/tmp/tests/example01) ==16385== by 0x44EC60: std::_Rb_tree&lt;unsigned long, std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt;, std::_Select1st&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::_M_erase(std::_Rb_tree_node&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;*) (in /home/maroy/tmp/tests/example01) ==16385== by 0x44EC93: std::_Rb_tree&lt;unsigned long, std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt;, std::_Select1st&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::clear() (in /home/maroy/tmp/tests/example01) ==16385== by 0x472A3E: std::_Rb_tree&lt;unsigned long, std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt;, std::_Select1st&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::erase(std::_Rb_tree_iterator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::_Rb_tree_iterator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;) (in /home/maroy/tmp/tests/example01) ==16385== by 0x472AC0: std::_Rb_tree&lt;unsigned long, std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt;, std::_Select1st&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt;, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::erase(unsigned long const&amp;) (in /home/maroy/tmp/tests/example01) ==16385== by 0x472AF2: std::map&lt;unsigned long, boost::unit_test::test_unit*, std::less&lt;unsigned long&gt;, std::allocator&lt;std::pair&lt;unsigned long const, boost::unit_test::test_unit*&gt; &gt; &gt;::erase(unsigned long const&amp;) (in /home/maroy/tmp/tests/example01) ==16385== by 0x42950D: boost::unit_test::framework::deregister_test_unit(boost::unit_test::test_unit*) (in /home/maroy/tmp/tests/example01) ==16385== by 0x4295D5: boost::unit_test::test_unit::~test_unit() (in /home/maroy/tmp/tests/example01) ==16385== by 0x472E21: boost::unit_test::test_case::~test_case() (in /home/maroy/tmp/tests/example01) ==16385== ==16385== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 8 from 1) ==16385== malloc/free: in use at exit: 0 bytes in 0 blocks. ==16385== malloc/free: 373 allocs, 373 frees, 51,963 bytes allocated. ==16385== For counts of detected errors, rerun with: -v ==16385== All heap blocks were freed -- no leaks are possible. </pre><p> output of google perf tools: </p> <pre class="wiki">$ g++ -ltcmalloc -lpthread -o example01 example01.cpp $ env HEAPCHECK=normal ./example01HeapProfiler: Removing old profile /tmp/example01.15966._main_-beg.heap HeapProfiler: Removing old profile /tmp/example01.15966._main_-end.heap HeapChecker: Starting check "_main_" HeapChecker: Ignoring 10 reachable objects of 415 bytes Running 1 test case... *** No errors detected HeapChecker: Checking for whole-program memory leaks HeapChecker: Ending check "_main_" HeapChecker: Ignoring 23 reachable objects of 1358 bytes HeapChecker: Heap memory leaks of 57 bytes and/or 1 allocations detected by check "_main_". HeapChecker: To investigate leaks manually use e.g. cd /home/maroy/tmp/tests; /usr/bin/pprof ./example01 "/tmp/example01.16547._main_-end.heap" --inuse_objects --lines --edgefraction=1e-10 --nodefraction=1e-10 --gv 2&gt;/dev/null HeapChecker: Below is this pprof's output: 1 100.0% 100.0% 1 100.0% 0x00007f156bca4011 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156bca57a3 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156c1400a6 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156bca5541 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156c1403d6 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156c141963 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156c143136 ??:0 0 0.0% 100.0% 1 100.0% 0x00007f156bca48bb ??:0 HeapChecker: crashing because of leaks Segmentation fault </pre><p> this is on an ubuntu 8.10 64 bit using g++ 4.3.2: </p> <pre class="wiki">$ uname -a Linux tomodachi 2.6.27-15-generic #1 SMP Tue Oct 20 06:50:36 UTC 2009 x86_64 GNU/Linux $ g++ --version g++ (Ubuntu 4.3.2-1ubuntu12) 4.3.2 Copyright (C) 2008 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. </pre> en-us Boost C++ Libraries /htdocs/site/boost.png https://svn.boost.org/trac10/ticket/3635 Trac 1.4.3 Gennadiy Rozental Mon, 23 Nov 2009 01:45:31 GMT status changed; resolution set https://svn.boost.org/trac10/ticket/3635#comment:1 https://svn.boost.org/trac10/ticket/3635#comment:1 <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">duplicate</span> </li> </ul> <p> Duplicates <a class="closed ticket" href="https://svn.boost.org/trac10/ticket/2889" title="#2889: Bugs: Unit Test framework does things that OS X's malloc dislikes (closed: fixed)">#2889</a> </p> Ticket