Valgrind报告泄漏与我的程序无关

问题描述 投票:0回答:1

Valgrind总是报告任何种类的C程序的内存泄漏,即使看起来没有问题的程序也是如此。似乎正在报告与我的程序无关的泄漏。以下是我运行的程序以及从Valgrind获得的报告。我正在用gcc编译程序。我尝试使用/不使用-g和-o0进行编译,但没有任何区别。为什么要报告这些事情,我应该/应该压制它们,以及如何?

关于我的环境:

System Version: macOS 10.13.1 (17B1003)
Kernel Version: Darwin 17.2.0

>gcc --version
Configured with: --prefix=/Library/Developer/CommandLineTools/usr --with-gxx-include-dir=/usr/include/c++/4.2.1
Apple LLVM version 9.1.0 (clang-902.0.39.2)
Target: x86_64-apple-darwin17.2.0
Thread model: posix
InstalledDir: /Library/Developer/CommandLineTools/usr/bin

>brew -v
Homebrew 2.1.16-127-ga1939cf
Homebrew/homebrew-core (git revision 059d; last commit 2019-11-24)
int     main(void)
{
    return (0);
}

--leak-check=full

==4665== Memcheck, a memory error detector
==4665== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==4665== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==4665== Command: ./leaktest2
==4665==
==4665==
==4665== HEAP SUMMARY:
==4665==     in use at exit: 18,484 bytes in 165 blocks
==4665==   total heap usage: 186 allocs, 21 frees, 26,932 bytes allocated
==4665==
==4665== 64 bytes in 1 blocks are definitely lost in loss record 14 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1001A7C04: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==4665==
==4665== 64 bytes in 1 blocks are definitely lost in loss record 15 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==
==4665== 64 bytes in 1 blocks are definitely lost in loss record 16 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC72: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==
==4665== 64 bytes in 1 blocks are definitely lost in loss record 17 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC72: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==
==4665== 72 bytes in 3 blocks are possibly lost in loss record 18 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BC7E2: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1001A7C04: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==4665==    by 0x1001A7BEB: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==4665==    by 0x1000B89C2: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==4665==    by 0x100018A09: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==4665==    by 0x100018C39: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==4665==
==4665== 80 (32 direct, 48 indirect) bytes in 1 blocks are definitely lost in loss record 19 of 39
==4665==    at 0x1000AA53E: malloc_zone_malloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BEAA1: NXCreateHashTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BEA50: NXCreateHashTable (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE950: NXCreateMapTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE8FA: NXCreateMapTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BD3CB: __sel_registerName(char const*, int, int) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BD0DA: sel_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BC9AD: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==
==4665== 144 (32 direct, 112 indirect) bytes in 1 blocks are definitely lost in loss record 22 of 39
==4665==    at 0x1000AA53E: malloc_zone_malloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BEA80: NXCreateHashTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BEA50: NXCreateHashTable (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE950: NXCreateMapTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE8FA: NXCreateMapTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BD3CB: __sel_registerName(char const*, int, int) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BD0DA: sel_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BC9AD: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==
==4665== 192 bytes in 3 blocks are definitely lost in loss record 23 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1001A7C04: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==4665==    by 0x1001A7BEB: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==4665==
==4665== 192 bytes in 3 blocks are definitely lost in loss record 24 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC72: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC5A: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1001A7C04: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==4665==
==4665== 3,264 bytes in 51 blocks are definitely lost in loss record 37 of 39
==4665==    at 0x1000AA92A: calloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BFBA4: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BFC72: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE363: _read_images (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BCAC4: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1001A7C04: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==4665==    by 0x1001A7BEB: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==4665==
==4665== 4,136 (24 direct, 4,112 indirect) bytes in 1 blocks are definitely lost in loss record 39 of 39
==4665==    at 0x1000AA53E: malloc_zone_malloc (in /nfs/2018/e/username/.brew/Cellar/valgrind/3.15.0/lib/valgrind/vgpreload_memcheck-amd64-darwin.so)
==4665==    by 0x1006BE928: NXCreateMapTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BE8FA: NXCreateMapTableFromZone (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BD3CB: __sel_registerName(char const*, int, int) (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BD0DA: sel_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006BC9AD: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1006CF7DA: objc_object::sidetable_retainCount() (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x100006C64: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), bool, bool) (in /usr/lib/dyld)
==4665==    by 0x100006E39: dyld::registerObjCNotifiers(void (*)(unsigned int, char const* const*, mach_header const* const*), void (*)(char const*, mach_header const*), void (*)(char const*, mach_header const*)) (in /usr/lib/dyld)
==4665==    by 0x10021D84D: _dyld_objc_notify_register (in /usr/lib/system/libdyld.dylib)
==4665==    by 0x1006BC075: _objc_init (in /usr/lib/libobjc.A.dylib)
==4665==    by 0x1001A7C04: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==4665==
==4665== LEAK SUMMARY:
==4665==    definitely lost: 3,992 bytes in 64 blocks
==4665==    indirectly lost: 6,864 bytes in 7 blocks
==4665==      possibly lost: 72 bytes in 3 blocks
==4665==    still reachable: 32 bytes in 1 blocks
==4665==         suppressed: 7,524 bytes in 90 blocks
==4665== Reachable blocks (those to which a pointer was found) are not shown.
==4665== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==4665==
==4665== For lists of detected and suppressed errors, rerun with: -s
==4665== ERROR SUMMARY: 11 errors from 11 contexts (suppressed: 12 from 12)
#include <stdio.h>

int     main(void)
{
    printf("hello\n");
    return (0);
}
==43631== Memcheck, a memory error detector
==43631== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==43631== Using Valgrind-3.15.0 and LibVEX; rerun with -h for copyright info
==43631== Command: ./leaktest2
==43631==
hello
==43631==
==43631== HEAP SUMMARY:
==43631==     in use at exit: 22,580 bytes in 166 blocks
==43631==   total heap usage: 187 allocs, 21 frees, 31,028 bytes allocated
==43631==
==43631== LEAK SUMMARY:
==43631==    definitely lost: 8,088 bytes in 65 blocks
==43631==    indirectly lost: 6,864 bytes in 7 blocks
==43631==      possibly lost: 72 bytes in 3 blocks
==43631==    still reachable: 32 bytes in 1 blocks
==43631==         suppressed: 7,524 bytes in 90 blocks
==43631== Rerun with --leak-check=full to see details of leaked memory
==43631==
==43631== For lists of detected and suppressed errors, rerun with: -s
==43631== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 4 from 4)
debugging memory-leaks valgrind
1个回答
0
投票

[看一下here,向下滚动到“添加抑制”。

简而言之,将--memcheck:gen-suppressions=all添加到第一次运行的memcheck,然后将抑制节添加到抑制文件中。然后在随后的memcheck运行中添加--memcheck:suppressions=<your suppression file>

您可以手动编写禁止文件,但是生成的文件更易于使用,因为如果需要,它将为您进行名称修改。

© www.soinside.com 2019 - 2024. All rights reserved.