Valgrind在Mac OS下如何检测内存泄漏

14

今天我在我的Mac OS X 10.6上安装了valgrind并尝试测试它。结果发现系统中有奇怪的内存泄漏。我所做的只是创建一个简单的c文件,获取一些堆内存并立即释放它。当我运行valgrind时,它显示了类似于以下内容:

Realfrees-MacBook-Pro:C Realfree$ valgrind --tool=memcheck --leak-check=yes --show-reachable=yes ./a.out 
==2621== Memcheck, a memory error detector
==2621== Copyright (C) 2002-2010, and GNU GPL'd, by Julian Seward et al.
==2621== Using Valgrind-3.6.0 and LibVEX; rerun with -h for copyright info
==2621== Command: ./a.out
==2621== 
--2621-- ./a.out:
--2621-- dSYM directory is missing; consider using --dsymutil=yes
==2621== 
==2621== HEAP SUMMARY:
==2621==     in use at exit: 88 bytes in 1 blocks
==2621==   total heap usage: 2 allocs, 1 frees, 92 bytes allocated
==2621== 
==2621== 88 bytes in 1 blocks are still reachable in loss record 1 of 1
==2621==    at 0x100010915: malloc (vg_replace_malloc.c:236)
==2621==    by 0x1000260EB: get_or_create_key_element (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x100026008: _keymgr_get_and_lock_processwide_ptr_2 (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x100025FCF: __keymgr_initializer (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x1000245E7: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==2621==    by 0x7FFF5FC0D4FF:  ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC0BCEB: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC0BC9C: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC0BDA5: ImageLoader::runInitializers(ImageLoader::LinkContext const&) (in /usr/lib/dyld) 
==2621==    by 0x7FFF5FC020EE: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC06980: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**) (in /usr/lib/dyld)
==2621==    by 0x7FFF5FC016D1: dyldbootstrap::start(macho_header const*, int, char const**, long) (in /usr/lib/dyld)
==2621== 
==2621== LEAK SUMMARY:
==2621==    definitely lost: 0 bytes in 0 blocks
==2621==    indirectly lost: 0 bytes in 0 blocks
==2621==      possibly lost: 0 bytes in 0 blocks
==2621==    still reachable: 88 bytes in 1 blocks
==2621==         suppressed: 0 bytes in 0 blocks
==2621== 
==2621== For counts of detected and suppressed errors, rerun with: -v
==2621== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

一定数量的内存仍然是可访问的。有什么解决办法吗?或者这在Mac上是正常现象吗?

另外,我使用命令“ls -l”运行了valgrind,并显示了一堆仍然可访问的块。 我不确定这在Mac上是否是正常行为。 以下是命令行“valgrind --tool=memcheck ls -l”的结果:

==2734== 
==2734== HEAP SUMMARY:
==2734==     in use at exit: 118,331 bytes in 52 blocks
==2734==   total heap usage: 1,253 allocs, 1,201 frees, 214,242 bytes allocated
==2734== 
==2734== LEAK SUMMARY:
==2734==    definitely lost: 0 bytes in 0 blocks
==2734==    indirectly lost: 0 bytes in 0 blocks
==2734==      possibly lost: 0 bytes in 0 blocks
==2734==    still reachable: 118,331 bytes in 52 blocks
==2734==         suppressed: 0 bytes in 0 blocks
==2734== Rerun with --leak-check=full to see details of leaked memory
==2734== 
==2734== For counts of detected and suppressed errors, rerun with: -v
==2734== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
2个回答

6

您可能需要使用适用于 Mac 的 Valgrind 抑制文件。似乎在进程退出时分配了一些进程范围内的内存,但没有释放。请尝试使用 valgrind --suppressions=<抑制文件路径>


5

2
有适用于10.9的吗? - Translunar
4
@Dr.JohnnyMohawk 最新的源代码应该都列在这里,包括用于 OS X 10.9 / Darwin 13.0 的源代码(请参见这里了解 Darwin 版本和 OS X 版本之间的对应关系,或者在终端中输入 uname -sr)。 - waldyrious

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接