在Mac OSX上进行C++内存泄漏检测

4
我正在使用Text Wrangler编写C++代码,并使用命令行中的g++进行编译。我想要检测我的代码是否存在内存泄漏。我尝试使用valgrind,但它似乎无法在Mavericks上正常工作。我尝试运行"leak a.out",其中a.out是运行"g++ *.h *.cc"的结果,但我收到错误消息"leaks cannot find a process you have access to which has a name like 'a.out'"。有什么办法可以进行内存泄漏检查吗?
==1317== Memcheck, a memory error detector
==1317== Copyright (C) 2002-2012, and GNU GPL'd, by Julian Seward et al.
==1317== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
==1317== Command: ./a.out
==1317== 
==1317== WARNING: Support on MacOS 10.8 is experimental and mostly broken.
==1317== WARNING: Expect incorrect results, assertions and crashes.
==1317== WARNING: In particular, Memcheck on 32-bit programs will fail to
==1317== WARNING: detect any errors associated with heap-allocated data.
==1317== 
--1317-- Valgrind options:
--1317--    --leak-check=full
--1317--    -v
--1317-- Contents of /proc/version:
--1317--   can't open /proc/version
--1317-- Arch and hwcaps: AMD64, amd64-sse3-cx16-avx
--1317-- Page sizes: currently 4096, max supported 4096
--1317-- Valgrind library directory: /usr/local/lib/valgrind
--1317-- ./a.out (rx at 0x100000000, rw at 0x100002000)
--1317--    reading syms   from primary file (14 7)
--1317--    dSYM directory is missing; consider using --dsymutil=yes
--1317-- /usr/lib/dyld (rx at 0x7fff5fc00000, rw at 0x7fff5fc34000)
--1317--    reading syms   from primary file (6 1183)
--1317-- Scheduler: using generic scheduler lock implementation.
--1317-- Reading suppressions file: /usr/local/lib/valgrind/default.supp
==1317== embedded gdbserver: reading from /var/folders/b8/2knh_y5j1q9gn4874f4c10yh0000gn/T//vgdb-pipe-from-vgdb-to-1317-by-joelmalchiondo-on-???
==1317== embedded gdbserver: writing to   /var/folders/b8/2knh_y5j1q9gn4874f4c10yh0000gn/T//vgdb-pipe-to-vgdb-from-1317-by-joelmalchiondo-on-???
==1317== embedded gdbserver: shared mem   /var/folders/b8/2knh_y5j1q9gn4874f4c10yh0000gn/T//vgdb-pipe-shared-mem-vgdb-1317-by-joelmalchiondo-on-???
==1317== 
==1317== TO CONTROL THIS PROCESS USING vgdb (which you probably
==1317== don't want to do, unless you know exactly what you're doing,
==1317== or are doing some strange experiment):
==1317==   /usr/local/lib/valgrind/../../bin/vgdb --pid=1317 ...command...
==1317== 
==1317== TO DEBUG THIS PROCESS USING GDB: start GDB like this
==1317==   /path/to/gdb ./a.out
==1317== and then give GDB the following command
==1317==   target remote | /usr/local/lib/valgrind/../../bin/vgdb --pid=1317
==1317== --pid is optional if only one valgrind process is running
==1317== 
--1317-- REDIR: 0x7fff5fc1b798 (arc4random) redirected to 0x238056b0e (???)
--1317-- REDIR: 0x7fff5fc21560 (strcmp) redirected to 0x238056a70 (???)
--1317-- REDIR: 0x7fff5fc1b560 (strlen) redirected to 0x238056a3f (???)
--1317-- REDIR: 0x7fff5fc1b4c0 (strcpy) redirected to 0x238056a8c (???)
--1317-- REDIR: 0x7fff5fc1ec4f (strcat) redirected to 0x238056a50 (???)
--1317-- /usr/local/lib/valgrind/vgpreload_core-amd64-darwin.so (rx at 0x1000, rw at 0x2000)
--1317--    reading syms   from primary file (3 31)
--1317--    dSYM= /usr/local/lib/valgrind/vgpreload_core-amd64-darwin.so.dSYM/Contents/Resources/DWARF/vgpreload_core-amd64-darwin.so
--1317--    reading dwarf3 from dsyms file
--1317-- /usr/local/lib/valgrind/vgpreload_memcheck-amd64-darwin.so (rx at 0x4000, rw at 0x8000)
--1317--    reading syms   from primary file (47 244)
--1317--    dSYM= /usr/local/lib/valgrind/vgpreload_memcheck-amd64-darwin.so.dSYM/Contents/Resources/DWARF/vgpreload_memcheck-amd64-darwin.so
--1317--    reading dwarf3 from dsyms file
UNKNOWN fcntl 61!--1317-- /usr/lib/libc++.1.dylib (rx at 0xc000, rw at 0x5f000)
--1317--    reading syms   from primary file (2070 1507)
UNKNOWN fcntl 61!--1317-- /usr/lib/libSystem.B.dylib (rx at 0xba000, rw at 0xbc000)
--1317--    reading syms   from primary file (31 4)
UNKNOWN fcntl 61!--1317-- /usr/lib/libc++abi.dylib (rx at 0xc2000, rw at 0xec000)
--1317--    reading syms   from primary file (322 178)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libcache.dylib (rx at 0xf9000, rw at 0xfe000)
--1317--    reading syms   from primary file (29 24)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libcommonCrypto.dylib (rx at 0x102000, rw at 0x10d000)
--1317--    reading syms   from primary file (203 183)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libcompiler_rt.dylib (rx at 0x11a000, rw at 0x122000)
--1317--    reading syms   from primary file (454 8)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libcopyfile.dylib (rx at 0x12d000, rw at 0x135000)
--1317--    reading syms   from primary file (11 28)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libcorecrypto.dylib (rx at 0x13b000, rw at 0x18a000)
--1317--    reading syms   from primary file (341 430)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libdispatch.dylib (rx at 0x19a000, rw at 0x1b5000)
--1317--    reading syms   from primary file (176 737)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libdyld.dylib (rx at 0x1ce000, rw at 0x1d2000)
--1317--    reading syms   from primary file (78 96)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libkeymgr.dylib (rx at 0x1d8000, rw at 0x1d9000)
--1317--    reading syms   from primary file (12 4)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/liblaunch.dylib (rx at 0x1dd000, rw at 0x1e5000)
--1317--    reading syms   from primary file (112 1)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libmacho.dylib (rx at 0x1ec000, rw at 0x1f2000)
--1317--    reading syms   from primary file (86 1)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libquarantine.dylib (rx at 0x1f7000, rw at 0x1fa000)
--1317--    reading syms   from primary file (66 32)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libremovefile.dylib (rx at 0x1ff000, rw at 0x201000)
--1317--    reading syms   from primary file (15 4)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_asl.dylib (rx at 0x206000, rw at 0x218000)
--1317--    reading syms   from primary file (127 106)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_blocks.dylib (rx at 0x221000, rw at 0x223000)
--1317--    reading syms   from primary file (25 23)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_c.dylib (rx at 0x227000, rw at 0x2b1000)
--1317--    reading syms   from primary file (1288 783)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_configuration.dylib (rx at 0x2dc000, rw at 0x2df000)
--1317--    reading syms   from primary file (27 58)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_dnssd.dylib (rx at 0x2e4000, rw at 0x2ed000)
--1317--    reading syms   from primary file (68 34)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_info.dylib (rx at 0x2f3000, rw at 0x31b000)
--1317--    reading syms   from primary file (526 526)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_kernel.dylib (rx at 0x330000, rw at 0x34d000)
--1317--    reading syms   from primary file (931 2486)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_m.dylib (rx at 0x36f000, rw at 0x39f000)
--1317--    reading syms   from primary file (573 1)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_malloc.dylib (rx at 0x3a9000, rw at 0x3c5000)
--1317--    reading syms   from primary file (102 199)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_network.dylib (rx at 0x3ce000, rw at 0x3f6000)
--1317--    reading syms   from primary file (168 1053)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_notify.dylib (rx at 0x411000, rw at 0x41b000)
--1317--    reading syms   from primary file (137 48)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_platform.dylib (rx at 0x423000, rw at 0x42a000)
--1317--    reading syms   from primary file (114 1463)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_pthread.dylib (rx at 0x43c000, rw at 0x444000)
--1317--    reading syms   from primary file (137 54)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_sandbox.dylib (rx at 0x44f000, rw at 0x451000)
--1317--    reading syms   from primary file (61 8)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libsystem_stats.dylib (rx at 0x455000, rw at 0x45a000)
--1317--    reading syms   from primary file (20 42)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libunc.dylib (rx at 0x460000, rw at 0x462000)
--1317--    reading syms   from primary file (31 4)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libunwind.dylib (rx at 0x467000, rw at 0x46d000)
--1317--    reading syms   from primary file (102 52)
UNKNOWN fcntl 61!--1317-- /usr/lib/system/libxpc.dylib (rx at 0x473000, rw at 0x498000)
--1317--    reading syms   from primary file (349 818)
UNKNOWN fcntl 61!--1317-- /usr/lib/libobjc.A.dylib (rx at 0x4b4000, rw at 0x662000)
--1317--    reading syms   from primary file (342 735)
UNKNOWN fcntl 61!--1317-- /usr/lib/libauto.dylib (rx at 0x682000, rw at 0x6c5000)
--1317--    reading syms   from primary file (68 742)
UNKNOWN fcntl 61!--1317-- /usr/lib/libDiagnosticMessagesClient.dylib (rx at 0x6dc000, rw at 0x6de000)
--1317--    reading syms   from primary file (21 14)
eq_SyscallStatus:
  {78 0 43}
  {78 0 40}

valgrind: m_syswrap/syswrap-main.c:380 (Bool eq_SyscallStatus(SyscallStatus *, SyscallStatus *)): the 'impossible' happened.
==1317==    at 0x23803C34B: ???
==1317==    by 0x23803C2F0: ???
==1317==    by 0x2380B1983: ???
==1317==    by 0x2380B107D: ???
==1317==    by 0x2380AF5F2: ???
==1317==    by 0x2380AD72A: ???
==1317==    by 0x2380BF1C0: ???

sched status:
  running_tid=1

Thread 1: status = VgTs_Runnable
==1317==    at 0x341966: _kernelrpc_mach_vm_map_trap (in /usr/lib/system/libsystem_kernel.dylib)
==1317==    by 0x3ABF6F: allocate_pages (in /usr/lib/system/libsystem_malloc.dylib)
==1317==    by 0x3ABA11: create_scalable_zone (in /usr/lib/system/libsystem_malloc.dylib)
==1317==    by 0x3B93ED: _malloc_initialize (in /usr/lib/system/libsystem_malloc.dylib)
==1317==    by 0x3BA27D: malloc (in /usr/lib/system/libsystem_malloc.dylib)
==1317==    by 0x7FFF5FC12D4F: malloc (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC1A3BD: operator new(unsigned long) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC090EC: std::__1::__split_buffer<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), std::__1::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)>&>::__split_buffer(unsigned long, unsigned long, std::__1::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)>&) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC08A44: std::__1::vector<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*), std::__1::allocator<char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)> >::insert(std::__1::__wrap_iter<char const* (* const*)(dyld_image_states, unsigned int, dyld_image_info const*)>, char const* (* const&)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0426B: dyld::registerImageStateBatchChangeHandler(dyld_image_states, char const* (*)(dyld_image_states, unsigned int, dyld_image_info const*)) (in /usr/lib/dyld)
==1317==    by 0x1D08F0: dyld_register_image_state_change_handler (in /usr/lib/system/libdyld.dylib)
==1317==    by 0x1D06B1: _dyld_initializer (in /usr/lib/system/libdyld.dylib)
==1317==    by 0xBBA9D: libSystem_initializer (in /usr/lib/libSystem.B.dylib)
==1317==    by 0x7FFF5FC11C2D: ImageLoaderMachO::doModInitFunctions(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC11DB9: ImageLoaderMachO::doInitialization(ImageLoader::LinkContext const&) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0EA61: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0E9EA: ImageLoader::recursiveInitialization(ImageLoader::LinkContext const&, unsigned int, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0E8F5: ImageLoader::runInitializers(ImageLoader::LinkContext const&, ImageLoader::InitializerTimingList&) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC021B6: dyld::initializeMainExecutable() (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0555F: dyld::_main(macho_header const*, unsigned long, int, char const**, char const**, char const**, unsigned long*) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0127A: dyldbootstrap::start(macho_header const*, int, char const**, long, macho_header const*, unsigned long*) (in /usr/lib/dyld)
==1317==    by 0x7FFF5FC0105D: _dyld_start (in /usr/lib/dyld)


Note: see also the FAQ in the source distribution.
It contains workarounds to several common problems.
In particular, if Valgrind aborted or crashed after
identifying problems in your program, there's a good chance
that fixing those problems will prevent Valgrind aborting or
crashing, especially if it happened in m_mallocfree.c.

If that doesn't help, please report this bug to: www.valgrind.org

In the bug report, send all the above text, the valgrind
version, and what OS and version you are using.  Thanks.

你尝试过使用 Instruments 吗?它应该随 Xcode 更新一起提供。 - WhozCraig
1
@WhozCraig 工具在启用系统完整性保护时无法工作。 - Franklin Yu
3个回答

2

如果你发布输出结果,我们可以帮助你理解它。或者尝试其他提供的工具。 - Rémi Benoit
==867== 警告:在MacOS 10.8上的支持是实验性的,且大部分功能失效。 ==867== 警告:请预期会出现不正确的结果、断言和崩溃。 ==867== 警告:特别地,在32位程序上运行Memcheck将会失败 t 0x39f000) --867-- 正在从主文件(573 1)读取符号 UNKNOWN fcntl 61!--867-- /usr/lib/system/libsystem_malloc.dylib (rx at 0x3a9000, rw at 0x3c5000) --867-- 正在从主文件(102 199)读取符号 UNKNOWN fcntl 61!--867-- /usr/lib/system/libsystem_network.dylib (rx at 0x3ce000, rw at 0x3f6000) - lufthansa747
这甚至不是全部,只是一行接着一行的无意义代码。 - lufthansa747
请发布完整的日志,如果您不理解,一个好的起点是Valgrind faq。虽然我没有尝试其他解决方案,但我相信它们中没有一个是魔法。找到泄漏可能是一件复杂的事情,因此有许多文章来解决这个问题。 - Rémi Benoit
2
需要注意的一件事是:在使用valgrind检查内存泄漏时,确保您的进程自然退出。如果您强制终止进程(例如通过control-c或“kill”命令),则会出现大量误报,因为该进程在退出之前没有机会释放任何内存。 - Jeremy Friesner
显示剩余3条评论

1

Valgrind可以,而且不会对程序造成干扰。

但是如果可能的话,使用AddressSanitizer会导致减速更少。

它包含在g++-4.8clang中,不记得是哪个版本。您需要使用标志-fsanitize=address编译。


3
AddressSanitizer 目前尚未在 Mac OS X 上能够检测内存泄漏。请继续关注。 - Glider
不,即使在Clang中也不行。我有一些差不多能用的补丁,但还需要打磨。对于回复晚了感到抱歉。 - Glider

0

您可以使用iprofilermanpage),这是一种在命令行实用程序上使用Instruments的方法。您可以在实用程序上运行它,它会创建一个.dtps包,然后可以在Instruments中打开进行检查。


@lufthansa747 我也是一样。手动运行iprofiler和Mac应用程序instruments泄漏,我看不到明显的泄漏... - Ghita

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