我在 OS X 10.8.1、Mountain Lion 上使用 Valgrind 3.8.0 版。关于与 10.8.1 的兼容性,Valgrind 的网站说(我的斜体字):
Valgrind 3.8.0 适用于{x86,amd64}-darwin(Mac OS X 10.6 和 10.7,对 10.8 的支持有限)。
那么,我知道 10.8.1 只有“有限支持”。尽管如此,这个错误报告说(斜体是我的):
这(最新的 3.8.0 版本)使Valgrind 能够编译并能够在 OSX 10.8 上运行小程序。但请注意,它仍然适用于更大的应用程序,并且根本没有正确检查 32 位程序(Memcheck 遗漏了大多数错误)。
没关系。所以 Valgrind 应该可以在 10.8.1 上工作,如果有气质的话。所以现在我的问题:
我能够让 Valgrind 在 10.8.1 上轻松编译,但是当我在几个小型 C 程序上运行它时,我看到了一些奇怪的结果。为了尝试减少问题的可能原因,我最终编写了以下“程序”:
int main () {
return 0;
}
我会说,这不是很令人兴奋,而且几乎没有错误的空间。然后,我编译并通过 Valgrind 运行它:
gcc testC.c
valgrind ./a.out
这是我的输出:
==45417== Command: ./a.out
==45417==
==45417== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==45417== WARNING: Expect incorrect results, assertions and crashes.
==45417== WARNING: In particular, Memcheck on 32-bit programs will fail to
==45417== WARNING: detect any errors associated with heap-allocated data.
==45417==
--45417-- ./a.out:
--45417-- dSYM directory is missing; consider using --dsymutil=yes
==45417==
==45417== HEAP SUMMARY:
==45417== in use at exit: 58,576 bytes in 363 blocks
==45417== total heap usage: 514 allocs, 151 frees, 62,442 bytes allocated
==45417==
==45417== LEAK SUMMARY:
==45417== definitely lost: 8,624 bytes in 14 blocks
==45417== indirectly lost: 1,168 bytes in 5 blocks
==45417== possibly lost: 4,925 bytes in 68 blocks
==45417== still reachable: 43,859 bytes in 276 blocks
==45417== suppressed: 0 bytes in 0 blocks
==45417== Rerun with --leak-check=full to see details of leaked memory
==45417==
==45417== For counts of detected and suppressed errors, rerun with: -v
==45417== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)
我知道 Valgrind 还没有准备好迎接 10.8.1 的黄金时段。尽管如此,我还是希望能够在这里使用它——我只需要在小程序上使用它,而且对于即时的结果来说,没有什么是关键任务。但很明显,它报告了一个似乎不太可能泄漏的程序中的大量泄漏。因此:
我应该怎么做才能解决这个问题?
其他信息:
- 添加一个故意泄露的整数确实会将“肯定丢失”的计数增加适当的 4 个字节。
- 同样,通过不释放内存而故意泄漏对 malloc 的调用确实会适当地增加堆分配计数。
- 使用标志编译
-g
然后运行到 Valgrind(以解决dSYM directory is missing
错误)确实会导致该错误消失,但不会改变报告大量内存泄漏的问题。