title: Stack-allocated std::string reports as memory leak by valgrind
body: <p>On a Linux machine, run this script to compile a small C++ application and run it under valgrind:</p>
  
  <pre><code>#!/bin/bash
  
  set -x -e
  
  cd /tmp
  
  cat &gt; main.cpp &lt;&lt;EOF
  #include &lt;stdlib.h&gt; // exit()
  #include &lt;string&gt;   // std::string
  int main(int argc, char *argv[], char *const envp[])
  {
    std::string the_temp_string("bla bla bla"); 
    exit(0);
    return 0;
  } // end main
  EOF
  
  lsb_release -d
  /usr/bin/g++ --version
  
  /usr/bin/g++  -MD -DDEBUG -g -ggdb -gstabs+ -O0  -fPIC  -Wall -Werror -Wsynth -Wno-comment -Wreturn-type   main.cpp -c -o main.o
  /usr/bin/g++  -MD -DDEBUG -g -ggdb -gstabs+ -O0  -fPIC  -Wall -Werror -Wsynth -Wno-comment -Wreturn-type   main.o -L. -L/usr/lib64 -lstdc++  -o main.exe   
  
  rm -f valgrind.xml
  /usr/bin/valgrind \
    --xml=yes \
    --xml-file=valgrind.xml \
    --demangle=yes \
    --gen-suppressions=all \
    --track-origins=yes \
    --leak-check=full \
    --show-reachable=no \
    --num-callers=40 \
    ./main.exe \
    &amp;&amp; cat valgrind.xml
  </code></pre>
  
  <p>The output of the above for my RHEL 6.8 machine gives:</p>
  
  <pre><code>+ cd /tmp
  + cat
  + lsb_release -d
  Description:    Red Hat Enterprise Linux Workstation release 6.8 (Santiago)
  + /usr/bin/g++ --version
  g++ (GCC) 4.4.7 20120313 (Red Hat 4.4.7-17)
  Copyright (C) 2010 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.
  
  + /usr/bin/g++ -MD -DDEBUG -g -ggdb -gstabs+ -O0 -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type main.cpp -c -o main.o
  + /usr/bin/g++ -MD -DDEBUG -g -ggdb -gstabs+ -O0 -fPIC -Wall -Werror -Wsynth -Wno-comment -Wreturn-type main.o -L. -L/usr/lib64 -lstdc++ -o main.exe
  + rm -f valgrind.xml
  + /usr/bin/valgrind --xml=yes --xml-file=valgrind.xml --demangle=yes --gen-suppressions=all --track-origins=yes --leak-check=full --show-reachable=no --num-callers=40 ./main.exe
  + cat valgrind.xml
  &lt;?xml version="1.0"?&gt;
  
  &lt;valgrindoutput&gt;
  
  &lt;protocolversion&gt;4&lt;/protocolversion&gt;
  &lt;protocoltool&gt;memcheck&lt;/protocoltool&gt;
  
  &lt;preamble&gt;
    &lt;line&gt;Memcheck, a memory error detector&lt;/line&gt;
    &lt;line&gt;Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.&lt;/line&gt;
    &lt;line&gt;Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info&lt;/line&gt;
    &lt;line&gt;Command: ./main.exe&lt;/line&gt;
  &lt;/preamble&gt;
  
  &lt;pid&gt;23040&lt;/pid&gt;
  &lt;ppid&gt;23010&lt;/ppid&gt;
  &lt;tool&gt;memcheck&lt;/tool&gt;
  
  &lt;args&gt;
    &lt;vargv&gt;
      &lt;exe&gt;/usr/bin/valgrind&lt;/exe&gt;
      &lt;arg&gt;--xml=yes&lt;/arg&gt;
      &lt;arg&gt;--xml-file=valgrind.xml&lt;/arg&gt;
      &lt;arg&gt;--demangle=yes&lt;/arg&gt;
      &lt;arg&gt;--gen-suppressions=all&lt;/arg&gt;
      &lt;arg&gt;--track-origins=yes&lt;/arg&gt;
      &lt;arg&gt;--leak-check=full&lt;/arg&gt;
      &lt;arg&gt;--show-reachable=no&lt;/arg&gt;
      &lt;arg&gt;--num-callers=40&lt;/arg&gt;
    &lt;/vargv&gt;
    &lt;argv&gt;
      &lt;exe&gt;./main.exe&lt;/exe&gt;
    &lt;/argv&gt;
  &lt;/args&gt;
  
  &lt;status&gt;
    &lt;state&gt;RUNNING&lt;/state&gt;
    &lt;time&gt;00:00:00:00.051 &lt;/time&gt;
  &lt;/status&gt;
  
  
  &lt;status&gt;
    &lt;state&gt;FINISHED&lt;/state&gt;
    &lt;time&gt;00:00:00:00.298 &lt;/time&gt;
  &lt;/status&gt;
  
  &lt;error&gt;
    &lt;unique&gt;0x6&lt;/unique&gt;
    &lt;tid&gt;1&lt;/tid&gt;
    &lt;kind&gt;Leak_PossiblyLost&lt;/kind&gt;
    &lt;xwhat&gt;
      &lt;text&gt;36 bytes in 1 blocks are possibly lost in loss record 1 of 1&lt;/text&gt;
      &lt;leakedbytes&gt;36&lt;/leakedbytes&gt;
      &lt;leakedblocks&gt;1&lt;/leakedblocks&gt;
    &lt;/xwhat&gt;
    &lt;stack&gt;
      &lt;frame&gt;
        &lt;ip&gt;0x4A075FC&lt;/ip&gt;
        &lt;obj&gt;/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so&lt;/obj&gt;
        &lt;fn&gt;operator new(unsigned long)&lt;/fn&gt;
        &lt;dir&gt;/builddir/build/BUILD/valgrind-3.8.1/coregrind/m_replacemalloc&lt;/dir&gt;
        &lt;file&gt;vg_replace_malloc.c&lt;/file&gt;
        &lt;line&gt;298&lt;/line&gt;
      &lt;/frame&gt;
      &lt;frame&gt;
        &lt;ip&gt;0x3138C9C3C8&lt;/ip&gt;
        &lt;obj&gt;/usr/lib64/libstdc++.so.6.0.13&lt;/obj&gt;
        &lt;fn&gt;std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator&amp;lt;char&amp;gt; const&amp;amp;)&lt;/fn&gt;
      &lt;/frame&gt;
      &lt;frame&gt;
        &lt;ip&gt;0x3138C9CDE4&lt;/ip&gt;
        &lt;obj&gt;/usr/lib64/libstdc++.so.6.0.13&lt;/obj&gt;
      &lt;/frame&gt;
      &lt;frame&gt;
        &lt;ip&gt;0x3138C9CF32&lt;/ip&gt;
        &lt;obj&gt;/usr/lib64/libstdc++.so.6.0.13&lt;/obj&gt;
        &lt;fn&gt;std::basic_string&amp;lt;char, std::char_traits&amp;lt;char&amp;gt;, std::allocator&amp;lt;char&amp;gt; &amp;gt;::basic_string(char const*, std::allocator&amp;lt;char&amp;gt; const&amp;amp;)&lt;/fn&gt;
      &lt;/frame&gt;
      &lt;frame&gt;
        &lt;ip&gt;0x40075C&lt;/ip&gt;
        &lt;obj&gt;/tmp/main.exe&lt;/obj&gt;
        &lt;fn&gt;main&lt;/fn&gt;
      &lt;/frame&gt;
    &lt;/stack&gt;
    &lt;suppression&gt;
      &lt;sname&gt;insert_a_suppression_name_here&lt;/sname&gt;
      &lt;skind&gt;Memcheck:Leak&lt;/skind&gt;
      &lt;sframe&gt; &lt;fun&gt;_Znwm&lt;/fun&gt; &lt;/sframe&gt;
      &lt;sframe&gt; &lt;fun&gt;_ZNSs4_Rep9_S_createEmmRKSaIcE&lt;/fun&gt; &lt;/sframe&gt;
      &lt;sframe&gt; &lt;obj&gt;/usr/lib64/libstdc++.so.6.0.13&lt;/obj&gt; &lt;/sframe&gt;
      &lt;sframe&gt; &lt;fun&gt;_ZNSsC1EPKcRKSaIcE&lt;/fun&gt; &lt;/sframe&gt;
      &lt;sframe&gt; &lt;fun&gt;main&lt;/fun&gt; &lt;/sframe&gt;
      &lt;rawtext&gt;
  &lt;![CDATA[
  {
     &lt;insert_a_suppression_name_here&gt;
     Memcheck:Leak
     fun:_Znwm
     fun:_ZNSs4_Rep9_S_createEmmRKSaIcE
     obj:/usr/lib64/libstdc++.so.6.0.13
     fun:_ZNSsC1EPKcRKSaIcE
     fun:main
  }
  ]]&gt;
      &lt;/rawtext&gt;
    &lt;/suppression&gt;
  &lt;/error&gt;
  
  &lt;errorcounts&gt;
  &lt;/errorcounts&gt;
  
  &lt;suppcounts&gt;
    &lt;pair&gt;
      &lt;count&gt;4&lt;/count&gt;
      &lt;name&gt;U1004-ARM-_dl_relocate_object&lt;/name&gt;
    &lt;/pair&gt;
    &lt;pair&gt;
      &lt;count&gt;2&lt;/count&gt;
      &lt;name&gt;glibc-2.5.x-on-SUSE-10.2-(PPC)-2a&lt;/name&gt;
    &lt;/pair&gt;
  &lt;/suppcounts&gt;
  
  &lt;/valgrindoutput&gt;
  </code></pre>
  
  <p>Applying the <code>GLIBCXX_FORCE_NEW</code> variable as indicated in the <a href="https://stackoverflow.com/a/7284726/257924">https://stackoverflow.com/a/7284726/257924</a> answer had no effect.</p>
  
  <p>So my question is this: Is this a bug in valgrind, in the C++ standard library version on this machine, or my own misunderstanding of how the C++ library gets torn down at/near <code>::exit()</code> call time?</p>
  
  <p>My current thinking is that I can should just suppress it in valgrind and move on.</p>