Valgrind C++内存在空文件中泄漏

Valgrind C++ memory leak in empty file

本文关键字:文件 泄漏 存在 C++ 内存 Valgrind      更新时间:2023-10-16

我在valgrind中遇到了"额外"内存泄漏的问题。例如,我创建了一个名为temp.cpp:的测试程序

int main() { return 0; }

在终端,我运行:

>> g++ -o temp.out temp.cpp
>> valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./temp.out

这会导致多次内存泄漏。这怎么可能呢?

==4438== LEAK SUMMARY:
==4438==    definitely lost: 4,120 bytes in 2 blocks
==4438==    indirectly lost: 2,288 bytes in 6 blocks
==4438==      possibly lost: 8,336 bytes in 99 blocks
==4438==    still reachable: 6,440 bytes in 13 blocks  
==4438==         suppressed: 5,020 bytes in 73 blocks

我试过运行其他.cpp文件,得到了完全相同的泄漏摘要。大约一个月前,当我尝试时,没有任何问题。我可能已经升级了Xcode或其他什么,如果这可能是问题的话(?)。这些是我对g++的设置:

配置:

--prefix=/Applications/Xcode.app/Contents/Developer/usr --with-gxx-include-dir=/Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.10.sdk/usr/include/c++/4.2.1

Apple LLVM版本6.1.0(clang-602.0.53)(基于LLVM 3.6.0svn)目标:x86_64-apple-darwin15.0.0线程模型:posix

看来valgrind在MacOSX上有问题。虽然这些问题尚未解决,但一个可能的临时解决方案是使用抑制文件。有关更多详细信息,请查看其他答案

我试图复制这个,但在我的机器上无法复制。最新的valgrind似乎没有泄漏。尝试酿造升级valgrind

C02QH2D7G8WM:simple userone$ valgrind --tool=memcheck --leak-check=full --show-leak-kinds=all ./temp.out
==1917== Memcheck, a memory error detector
==1917== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==1917== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==1917== Command: ./temp.out
==1917== 
==1917== 
==1917== HEAP SUMMARY:
==1917==     in use at exit: 22,177 bytes in 189 blocks
==1917==   total heap usage: 255 allocs, 66 frees, 27,953 bytes allocated
==1917== 
==1917== 2,064 bytes in 1 blocks are possibly lost in loss record 57 of 62
==1917==    at 0x10000817C: malloc_zone_malloc (in /usr/local/Cellar/valgrind/3.11.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==1917==    by 0x1005E0EFD: _objc_copyClassNamesForImage (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D4182: protocols() (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D4093: readClass(objc_class*, bool, bool) (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D1C13: gc_init (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D924E: objc_initializeClassPair_internal(objc_class*, char const*, objc_class*, objc_class*) (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005E6132: layout_string_create (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D483C: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D4300: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D42E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D42E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1917==    by 0x1005D42E9: copySwiftV1MangledName(char const*, bool) (in /usr/lib/libobjc.A.dylib)
==1917== 
==1917== LEAK SUMMARY:
==1917==    definitely lost: 0 bytes in 0 blocks
==1917==    indirectly lost: 0 bytes in 0 blocks
==1917==      possibly lost: 2,064 bytes in 1 blocks
==1917==    still reachable: 0 bytes in 0 blocks
==1917==         suppressed: 20,113 bytes in 188 blocks
==1917== 
==1917== For counts of detected and suppressed errors, rerun with: -v
==1917== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 17 from 17)