Valgrind - 堆统计很奇怪:内存泄漏?

时间:2015-12-01 00:25:53

标签: c macos memory-leaks valgrind

Valgrind给了我疯狂的高数字,用于释放,分配和分配的字节数,以及没有意识到我已经在堆中释放了内存。

#include <stdio.h>
#include <stdlib.h>

int main(){
    return 0;
}

gcc -std=c99 -g -Werror -Wextra -pedantic valgrind.c
valgrind --leak-check=full ./a.out
==44611== Memcheck, a memory error detector
==44611== Copyright (C) 2002-2015, and GNU GPL'd, by Julian Seward et al.
==44611== Using Valgrind-3.11.0 and LibVEX; rerun with -h for copyright info
==44611== Command: ./a.out
==44611== 
==44611== 
==44611== HEAP SUMMARY:
==44611==     in use at exit: 22,223 bytes in 183 blocks
==44611==   total heap usage: 259 allocs, 76 frees, 28,335 bytes allocated
==44611== 
==44611== LEAK SUMMARY:
==44611==    definitely lost: 0 bytes in 0 blocks
==44611==    indirectly lost: 0 bytes in 0 blocks
==44611==      possibly lost: 0 bytes in 0 blocks
==44611==    still reachable: 0 bytes in 0 blocks
==44611==         suppressed: 22,223 bytes in 183 blocks
==44611== 
==44611== For counts of detected and suppressed errors, rerun with: -v
==44611== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 16 from 16)

它告诉我,当我的程序中没有任何内容时,我259 allocs, 76 frees, 28,335 bytes allocated是疯狂的。我害怕前几天永远不会释放malloced空间造成内存泄漏。如果是这样,任何想法如何我可以回收我Mac上以前的malloced内存?我正在使用El Capitan。

1 个答案:

答案 0 :(得分:3)

正如我在评论中指出的那样,对于Mac OS X来说这是半正常的,至少在您调整系统之前是这样。我不得不为El Capitan添加相当多的压制(由Valgrind生成)。

首先要注意的是,您的代码不分配内存,因此您的代码无法泄漏任何内存。任何泄漏或可能泄露的东西都是在程序启动之前由操作系统完成的。

接下来需要注意的是,即使您决定担心它,也不用担心或者您可以做些什么。到目前为止,你最好的选择是让Valgrind知道你对此有所了解并继续生活。

那么,你怎么告诉Valgrind?

第1阶段正在使用--leak-check=full--num-callers=100运行Valgrind。例如,使用您的程序leaks.c在我的El Capitan上运行,我得到了:

==43058== Memcheck, a memory error detector
==43058== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==43058== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==43058== Command: leaks
==43058== 
--43058-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--43058-- UNKNOWN fcntl 97!
--43058-- UNKNOWN fcntl 97! (repeated 2 times)
--43058-- UNKNOWN fcntl 97! (repeated 4 times)
--43058-- UNKNOWN fcntl 97! (repeated 8 times)
--43058-- UNKNOWN fcntl 97! (repeated 16 times)
--43058-- UNKNOWN fcntl 97! (repeated 32 times)
--43058-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--43058-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--43058-- UNKNOWN task message [id 3406, to mach_task_self(), reply 0x60f]
--43058-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
--43058-- UNKNOWN host message [id 412, to mach_host_self(), reply 0x60f]
--43058-- UNKNOWN host message [id 222, to mach_host_self(), reply 0x60f]
--43058-- UNKNOWN task message [id 3410, to mach_task_self(), reply 0x60f]
==43058== 
==43058== HEAP SUMMARY:
==43058==     in use at exit: 22,245 bytes in 187 blocks
==43058==   total heap usage: 263 allocs, 76 frees, 28,341 bytes allocated
==43058== 
==43058== LEAK SUMMARY:
==43058==    definitely lost: 4,416 bytes in 4 blocks
==43058==    indirectly lost: 2,288 bytes in 6 blocks
==43058==      possibly lost: 7,096 bytes in 68 blocks
==43058==    still reachable: 8,445 bytes in 109 blocks
==43058==         suppressed: 0 bytes in 0 blocks
==43058== Rerun with --leak-check=full to see details of leaked memory
==43058== 
==43058== For counts of detected and suppressed errors, rerun with: -v
==43058== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

我不喜欢那些UNKNOWN消息,但这可能意味着我需要重建。请注意,我从SVN获得了代码 - 请参阅版本号。

现在,再次运行:

$ valgrind --num-callers=100 --leak-check=full ./leaks

==43059== Memcheck, a memory error detector
==43059== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==43059== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==43059== Command: ./leaks
==43059== 
--43059-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--43059-- UNKNOWN fcntl 97!
--43059-- UNKNOWN fcntl 97! (repeated 2 times)
--43059-- UNKNOWN fcntl 97! (repeated 4 times)
--43059-- UNKNOWN fcntl 97! (repeated 8 times)
--43059-- UNKNOWN fcntl 97! (repeated 16 times)
--43059-- UNKNOWN fcntl 97! (repeated 32 times)
--43059-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--43059-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--43059-- UNKNOWN task message [id 3406, to mach_task_self(), reply 0x60f]
--43059-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
--43059-- UNKNOWN host message [id 412, to mach_host_self(), reply 0x60f]
--43059-- UNKNOWN host message [id 222, to mach_host_self(), reply 0x60f]
--43059-- UNKNOWN task message [id 3410, to mach_task_self(), reply 0x60f]
==43059== 
==43059== HEAP SUMMARY:
==43059==     in use at exit: 22,245 bytes in 187 blocks
==43059==   total heap usage: 263 allocs, 76 frees, 28,341 bytes allocated
==43059== 
==43059== 24 bytes in 1 blocks are possibly lost in loss record 9 of 62
==43059==    at 0x100006355: malloc_zone_calloc (vg_replace_malloc.c:632)
==43059==    by 0x1004FE4B1: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FE1BE: NXCreateHashTableFromZone (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FE053: NXCreateHashTable (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FD037: _read_images (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FBB4D: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FB538: map_2_images (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x7FFF5FC047CF: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC04516: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43059==    by 0x10016689D: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==43059==    by 0x1004FB07A: _objc_init (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x10010EC4B: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==43059==    by 0x10010EC37: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==43059==    by 0x100010A01: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==43059==    by 0x7FFF5FC12F1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC13093: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F6CC: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F661: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F552: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F7C2: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC020AA: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC05D46: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC01275: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC01035: _dyld_start (in /usr/lib/dyld)
==43059== 
==43059== 24 bytes in 1 blocks are possibly lost in loss record 10 of 62
==43059==    at 0x100006355: malloc_zone_calloc (vg_replace_malloc.c:632)
==43059==    by 0x1004FE4B1: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x100500223: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FD90C: _read_images (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FBB4D: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x1004FB538: map_2_images (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x7FFF5FC047CF: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC04516: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43059==    by 0x10016689D: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==43059==    by 0x1004FB07A: _objc_init (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x10010EC4B: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==43059==    by 0x10010EC37: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==43059==    by 0x100010A01: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==43059==    by 0x7FFF5FC12F1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC13093: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F6CC: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F661: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F552: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F7C2: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC020AA: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC05D46: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC01275: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC01035: _dyld_start (in /usr/lib/dyld)
==43059== 

…lots of text missing…

==43059== 
==43059== 4,096 bytes in 1 blocks are definitely lost in loss record 62 of 62
==43059==    at 0x100007110: malloc_zone_memalign (vg_replace_malloc.c:769)
==43059==    by 0x100504D71: (anonymous namespace)::AutoreleasePoolPage::autoreleaseNoPage(objc_object*) (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x100504CF5: objc_autoreleasePoolPush (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x100504998: call_load_methods (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x100501B9D: load_images (in /usr/lib/libobjc.A.dylib)
==43059==    by 0x7FFF5FC01DE0: dyld::notifySingle(dyld_image_states, ImageLoader const*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F6B9: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F552: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F580: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC0F7C2: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC020AA: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC05D46: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC01275: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==43059==    by 0x7FFF5FC01035: _dyld_start (in /usr/lib/dyld)
==43059== 
==43059== LEAK SUMMARY:
==43059==    definitely lost: 4,416 bytes in 4 blocks
==43059==    indirectly lost: 2,288 bytes in 6 blocks
==43059==      possibly lost: 7,096 bytes in 68 blocks
==43059==    still reachable: 8,445 bytes in 109 blocks
==43059==         suppressed: 0 bytes in 0 blocks
==43059== Reachable blocks (those to which a pointer was found) are not shown.
==43059== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==43059== 
==43059== For counts of detected and suppressed errors, rerun with: -v
==43059== ERROR SUMMARY: 17 errors from 17 contexts (suppressed: 0 from 0)

当您查看这些堆栈跟踪时,您可以看到其中没有main条目。这表明问题不在您的代码中;它在系统代码中。因此,您再次使用--gen-suppressions=all重新运行Valgrind,并获得如下输出:

$ valgrind --gen-suppressions=all --num-callers=100 --leak-check=full ./leaks
==43060== Memcheck, a memory error detector
==43060== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==43060== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==43060== Command: ./leaks
==43060== 
--43060-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--43060-- UNKNOWN fcntl 97!
--43060-- UNKNOWN fcntl 97! (repeated 2 times)
--43060-- UNKNOWN fcntl 97! (repeated 4 times)
--43060-- UNKNOWN fcntl 97! (repeated 8 times)
--43060-- UNKNOWN fcntl 97! (repeated 16 times)
--43060-- UNKNOWN fcntl 97! (repeated 32 times)
--43060-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--43060-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--43060-- UNKNOWN task message [id 3406, to mach_task_self(), reply 0x60f]
--43060-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
--43060-- UNKNOWN host message [id 412, to mach_host_self(), reply 0x60f]
--43060-- UNKNOWN host message [id 222, to mach_host_self(), reply 0x60f]
--43060-- UNKNOWN task message [id 3410, to mach_task_self(), reply 0x60f]
==43060== 
==43060== HEAP SUMMARY:
==43060==     in use at exit: 22,245 bytes in 187 blocks
==43060==   total heap usage: 263 allocs, 76 frees, 28,341 bytes allocated
==43060== 
==43060== 24 bytes in 1 blocks are possibly lost in loss record 9 of 62
==43060==    at 0x100006355: malloc_zone_calloc (vg_replace_malloc.c:632)
==43060==    by 0x1004FE4B1: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FE1BE: NXCreateHashTableFromZone (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FE053: NXCreateHashTable (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FD037: _read_images (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FBB4D: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FB538: map_2_images (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x7FFF5FC047CF: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC04516: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43060==    by 0x10016689D: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==43060==    by 0x1004FB07A: _objc_init (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x10010EC4B: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==43060==    by 0x10010EC37: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==43060==    by 0x100010A01: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==43060==    by 0x7FFF5FC12F1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC13093: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F6CC: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F661: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F552: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F7C2: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC020AA: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC05D46: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC01275: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC01035: _dyld_start (in /usr/lib/dyld)
==43060== 
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: possible
   fun:malloc_zone_calloc
   fun:NXHashInsert
   fun:NXCreateHashTableFromZone
   fun:NXCreateHashTable
   fun:_read_images
   fun:map_images_nolock
   fun:map_2_images
   fun:_ZN4dyldL18notifyBatchPartialE17dyld_image_statesbPFPKcS0_jPK15dyld_image_infoE
   fun:_ZN4dyld36registerImageStateBatchChangeHandlerE17dyld_image_statesPFPKcS0_jPK15dyld_image_infoE
   fun:dyld_register_image_state_change_handler
   fun:_objc_init
   fun:_os_object_init
   fun:libdispatch_init
   fun:libSystem_initializer
   fun:_ZN16ImageLoaderMachO18doModInitFunctionsERKN11ImageLoader11LinkContextE
   fun:_ZN16ImageLoaderMachO16doInitializationERKN11ImageLoader11LinkContextE
   fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader15runInitializersERKNS_11LinkContextERNS_21InitializerTimingListE
   fun:_ZN4dyld24initializeMainExecutableEv
   fun:_ZN4dyld5_mainEPK12macho_headermiPPKcS5_S5_Pm
   fun:_ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
   fun:_dyld_start
}
==43060== 24 bytes in 1 blocks are possibly lost in loss record 10 of 62
==43060==    at 0x100006355: malloc_zone_calloc (vg_replace_malloc.c:632)
==43060==    by 0x1004FE4B1: NXHashInsert (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x100500223: realizeClass(objc_class*) (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FD90C: _read_images (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FBB4D: map_images_nolock (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x1004FB538: map_2_images (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x7FFF5FC047CF: dyld::notifyBatchPartial(dyld_image_states, bool, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC04516: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==43060==    by 0x10016689D: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==43060==    by 0x1004FB07A: _objc_init (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x10010EC4B: _os_object_init (in /usr/lib/system/libdispatch.dylib)
==43060==    by 0x10010EC37: libdispatch_init (in /usr/lib/system/libdispatch.dylib)
==43060==    by 0x100010A01: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==43060==    by 0x7FFF5FC12F1A: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC13093: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F6CC: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F661: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F552: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F7C2: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC020AA: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC05D46: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC01275: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC01035: _dyld_start (in /usr/lib/dyld)
==43060== 

…lots of material omitted again…

==43060== 4,096 bytes in 1 blocks are definitely lost in loss record 62 of 62
==43060==    at 0x100007110: malloc_zone_memalign (vg_replace_malloc.c:769)
==43060==    by 0x100504D71: (anonymous namespace)::AutoreleasePoolPage::autoreleaseNoPage(objc_object*) (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x100504CF5: objc_autoreleasePoolPush (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x100504998: call_load_methods (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x100501B9D: load_images (in /usr/lib/libobjc.A.dylib)
==43060==    by 0x7FFF5FC01DE0: dyld::notifySingle(dyld_image_states, ImageLoader const*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F6B9: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F552: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F580: ImageLoader::processInitializers(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&, ImageLoader::UninitedUpwards&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC0F7C2: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC020AA: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC05D46: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC01275: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==43060==    by 0x7FFF5FC01035: _dyld_start (in /usr/lib/dyld)
==43060== 
{
   <insert_a_suppression_name_here>
   Memcheck:Leak
   match-leak-kinds: definite
   fun:malloc_zone_memalign
   fun:_ZN12_GLOBAL__N_119AutoreleasePoolPage17autoreleaseNoPageEP11objc_object
   fun:objc_autoreleasePoolPush
   fun:call_load_methods
   fun:load_images
   fun:_ZN4dyldL12notifySingleE17dyld_image_statesPK11ImageLoader
   fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader15runInitializersERKNS_11LinkContextERNS_21InitializerTimingListE
   fun:_ZN4dyld24initializeMainExecutableEv
   fun:_ZN4dyld5_mainEPK12macho_headermiPPKcS5_S5_Pm
   fun:_ZN13dyldbootstrap5startEPK12macho_headeriPPKclS2_Pm
   fun:_dyld_start
}
==43060== LEAK SUMMARY:
==43060==    definitely lost: 4,416 bytes in 4 blocks
==43060==    indirectly lost: 2,288 bytes in 6 blocks
==43060==      possibly lost: 7,096 bytes in 68 blocks
==43060==    still reachable: 8,445 bytes in 109 blocks
==43060==         suppressed: 0 bytes in 0 blocks
==43060== Reachable blocks (those to which a pointer was found) are not shown.
==43060== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==43060== 
==43060== For counts of detected and suppressed errors, rerun with: -v
==43060== ERROR SUMMARY: 17 errors from 17 contexts (suppressed: 0 from 0)
$

然后将压缩收集到文件中,用更有用的东西替换<insert_a_suppression_name_here>。例如,我之前创建了一个条目:

{
   Mac-OS-X-El-Capitan-System-Leak.019
   Memcheck:Leak
   match-leak-kinds: definite
   fun:malloc_zone_memalign
   fun:_ZN12_GLOBAL__N_119AutoreleasePoolPage17autoreleaseNoPageEP11objc_object
   fun:objc_autoreleasePoolPush
   fun:call_load_methods
   fun:load_images
   fun:_ZN4dyldL12notifySingleE17dyld_image_statesPK11ImageLoader
   fun:_ZN11ImageLoader23recursiveInitializationERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader19processInitializersERKNS_11LinkContextEjRNS_21InitializerTimingListERNS_15UninitedUpwardsE
   fun:_ZN11ImageLoader15runInitializersERKNS_11LinkContextERNS_21InitializerTimingListE
   fun:_ZN4dyld24initializeMainExecutableEv
   fun:_ZN4dyld5_mainEPK12macho_headermiPPKcS5_S5_Pm
}

它保存在文件suppressions中。如果我使用我之前的抑制文件运行代码(我碰巧与leaks代码位于同一目录中),我得到:

$ valgrind --suppressions=suppressions --leak-check=full --num-callers=100 ./leaks
==43063== Memcheck, a memory error detector
==43063== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==43063== Using Valgrind-3.11.0.SVN and LibVEX; rerun with -h for copyright info
==43063== Command: ./leaks
==43063== 
--43063-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option
--43063-- UNKNOWN fcntl 97!
--43063-- UNKNOWN fcntl 97! (repeated 2 times)
--43063-- UNKNOWN fcntl 97! (repeated 4 times)
--43063-- UNKNOWN fcntl 97! (repeated 8 times)
--43063-- UNKNOWN fcntl 97! (repeated 16 times)
--43063-- UNKNOWN fcntl 97! (repeated 32 times)
--43063-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 2 times)
--43063-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 4 times)
--43063-- UNKNOWN task message [id 3406, to mach_task_self(), reply 0x60f]
--43063-- UNKNOWN mach_msg unhandled MACH_SEND_TRAILER option (repeated 8 times)
--43063-- UNKNOWN host message [id 412, to mach_host_self(), reply 0x60f]
--43063-- UNKNOWN host message [id 222, to mach_host_self(), reply 0x60f]
--43063-- UNKNOWN task message [id 3410, to mach_task_self(), reply 0x60f]
==43063== 
==43063== HEAP SUMMARY:
==43063==     in use at exit: 22,245 bytes in 187 blocks
==43063==   total heap usage: 263 allocs, 76 frees, 28,341 bytes allocated
==43063== 
==43063== LEAK SUMMARY:
==43063==    definitely lost: 0 bytes in 0 blocks
==43063==    indirectly lost: 2,288 bytes in 6 blocks
==43063==      possibly lost: 0 bytes in 0 blocks
==43063==    still reachable: 8,445 bytes in 109 blocks
==43063==         suppressed: 11,512 bytes in 72 blocks
==43063== Reachable blocks (those to which a pointer was found) are not shown.
==43063== To see them, rerun with: --leak-check=full --show-leak-kinds=all
==43063== 
==43063== For counts of detected and suppressed errors, rerun with: -v
==43063== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 17 from 17)
$

你可以看到我需要做一些工作来抑制一些分配,但是没有泄漏声称。

这大致也是你需要做的事情。您应该能够与Valgrind一起安装抑制,这样您就不必每次都指定--suppressions=suppressions

(我必须修剪输出以适应SO的30,000个字符限制。)