堆栈分配的std :: string报告由valgrind作为内存泄漏

时间:2017-08-25 02:10:07

标签: valgrind exit stdstring c++-standard-library

在Linux机器上,运行此脚本以编译一个小型C ++应用程序并在valgrind下运行它:

#!/bin/bash

set -x -e

cd /tmp

cat > main.cpp <<EOF
#include <stdlib.h> // exit()
#include <string>   // 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 \
  && cat valgrind.xml

我的RHEL 6.8机器的上述输出给出了:

+ 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
<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: ./main.exe</line>
</preamble>

<pid>23040</pid>
<ppid>23010</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind</exe>
    <arg>--xml=yes</arg>
    <arg>--xml-file=valgrind.xml</arg>
    <arg>--demangle=yes</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--track-origins=yes</arg>
    <arg>--leak-check=full</arg>
    <arg>--show-reachable=no</arg>
    <arg>--num-callers=40</arg>
  </vargv>
  <argv>
    <exe>./main.exe</exe>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.051 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:00:00.298 </time>
</status>

<error>
  <unique>0x6</unique>
  <tid>1</tid>
  <kind>Leak_PossiblyLost</kind>
  <xwhat>
    <text>36 bytes in 1 blocks are possibly lost in loss record 1 of 1</text>
    <leakedbytes>36</leakedbytes>
    <leakedblocks>1</leakedblocks>
  </xwhat>
  <stack>
    <frame>
      <ip>0x4A075FC</ip>
      <obj>/usr/lib64/valgrind/vgpreload_memcheck-amd64-linux.so</obj>
      <fn>operator new(unsigned long)</fn>
      <dir>/builddir/build/BUILD/valgrind-3.8.1/coregrind/m_replacemalloc</dir>
      <file>vg_replace_malloc.c</file>
      <line>298</line>
    </frame>
    <frame>
      <ip>0x3138C9C3C8</ip>
      <obj>/usr/lib64/libstdc++.so.6.0.13</obj>
      <fn>std::string::_Rep::_S_create(unsigned long, unsigned long, std::allocator&lt;char&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x3138C9CDE4</ip>
      <obj>/usr/lib64/libstdc++.so.6.0.13</obj>
    </frame>
    <frame>
      <ip>0x3138C9CF32</ip>
      <obj>/usr/lib64/libstdc++.so.6.0.13</obj>
      <fn>std::basic_string&lt;char, std::char_traits&lt;char&gt;, std::allocator&lt;char&gt; &gt;::basic_string(char const*, std::allocator&lt;char&gt; const&amp;)</fn>
    </frame>
    <frame>
      <ip>0x40075C</ip>
      <obj>/tmp/main.exe</obj>
      <fn>main</fn>
    </frame>
  </stack>
  <suppression>
    <sname>insert_a_suppression_name_here</sname>
    <skind>Memcheck:Leak</skind>
    <sframe> <fun>_Znwm</fun> </sframe>
    <sframe> <fun>_ZNSs4_Rep9_S_createEmmRKSaIcE</fun> </sframe>
    <sframe> <obj>/usr/lib64/libstdc++.so.6.0.13</obj> </sframe>
    <sframe> <fun>_ZNSsC1EPKcRKSaIcE</fun> </sframe>
    <sframe> <fun>main</fun> </sframe>
    <rawtext>
<![CDATA[
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   fun:_Znwm
   fun:_ZNSs4_Rep9_S_createEmmRKSaIcE
   obj:/usr/lib64/libstdc++.so.6.0.13
   fun:_ZNSsC1EPKcRKSaIcE
   fun:main
}
]]>
    </rawtext>
  </suppression>
</error>

<errorcounts>
</errorcounts>

<suppcounts>
  <pair>
    <count>4</count>
    <name>U1004-ARM-_dl_relocate_object</name>
  </pair>
  <pair>
    <count>2</count>
    <name>glibc-2.5.x-on-SUSE-10.2-(PPC)-2a</name>
  </pair>
</suppcounts>

</valgrindoutput>

应用https://stackoverflow.com/a/7284726/257924答案中指示的GLIBCXX_FORCE_NEW变量无效。

所以我的问题是:这是valgrind中的一个错误,在这台机器上的C ++标准库版本中,还是我自己对C ++库在::exit()调用时间/附近被拆除的误解? / p>

我目前的想法是,我可以在valgrind中压制它并继续前进。

2 个答案:

答案 0 :(得分:1)

您正在使用非常旧的valgrind版本(3.8.1),因此您无法从新的改进功能中受益。一些valgrind发布之前,一些c ++相关的启发式被添加到Valgrind,所以作为a.o.更好地理解std :: string。

请注意,xml输出包含的信息少于文本输出(例如,没有提供有关使用的启发式的详细信息)。

valgrind的最后一个版本给出了以下输出,表明使用stdstring启发式识别了std :: string。 在http://www.valgrind.org/docs/manual/mc-manual.html#mc-manual.leaks

查看有关启发式方法的更多详细信息
==10282== Memcheck, a memory error detector
==10282== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==10282== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==10282== Command: ./g
==10282== 
==10282== 
==10282== HEAP SUMMARY:
==10282==     in use at exit: 36 bytes in 1 blocks
==10282==   total heap usage: 1 allocs, 0 frees, 36 bytes allocated
==10282== 
==10282== LEAK SUMMARY:
==10282==    definitely lost: 0 bytes in 0 blocks
==10282==    indirectly lost: 0 bytes in 0 blocks
==10282==      possibly lost: 0 bytes in 0 blocks
==10282==    still reachable: 36 bytes in 1 blocks
==10282==                       of which reachable via heuristic:
==10282==                         stdstring          : 36 bytes in 1 blocks
==10282==         suppressed: 0 bytes in 0 blocks
==10282== Rerun with --leak-check=full to see details of leaked memory
==10282== 
==10282== For counts of detected and suppressed errors, rerun with: -v
==10282== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

答案 1 :(得分:0)

必须是标准C ++库中的错误或者我无法控制的其他错误,因为我在Ubuntu框中重试了脚本并得到了:

+ cd /tmp
+ cat
+ lsb_release -d
Description:    Ubuntu 17.04
+ /usr/bin/g++ --version
g++ (Ubuntu 6.3.0-12ubuntu2) 6.3.0 20170406
Copyright (C) 2016 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
<?xml version="1.0"?>

<valgrindoutput>

<protocolversion>4</protocolversion>
<protocoltool>memcheck</protocoltool>

<preamble>
  <line>Memcheck, a memory error detector</line>
  <line>Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.</line>
  <line>Using Valgrind-3.12.0 and LibVEX; rerun with -h for copyright info</line>
  <line>Command: ./main.exe</line>
</preamble>

<pid>17842</pid>
<ppid>17831</ppid>
<tool>memcheck</tool>

<args>
  <vargv>
    <exe>/usr/bin/valgrind.bin</exe>
    <arg>--xml=yes</arg>
    <arg>--xml-file=valgrind.xml</arg>
    <arg>--demangle=yes</arg>
    <arg>--gen-suppressions=all</arg>
    <arg>--track-origins=yes</arg>
    <arg>--leak-check=full</arg>
    <arg>--show-reachable=no</arg>
    <arg>--num-callers=40</arg>
  </vargv>
  <argv>
    <exe>./main.exe</exe>
  </argv>
</args>

<status>
  <state>RUNNING</state>
  <time>00:00:00:00.040 </time>
</status>


<status>
  <state>FINISHED</state>
  <time>00:00:00:00.551 </time>
</status>

<errorcounts>
</errorcounts>

<suppcounts>
</suppcounts>

</valgrindoutput>