c ++ Mac OSX上的内存泄漏检测

时间:2013-10-31 06:21:35

标签: c++ macos valgrind memory-leaks

我使用text wrangler在c ++中编写代码来编写代码并使用命令行中的g ++编译代码。我希望能够检测我的代码是否有任何内存泄漏。我已经尝试过使用valgrind,但它似乎对小牛队没有正常工作,我尝试运行“leak a.out”,其中a.out是运行“g ++ * .h * .cc”的结果,但我收到错误消息“泄漏找不到你有权访问的进程,其名称如'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.

3 个答案:

答案 0 :(得分:2)

Valgrind可以运行你的程序并使用

进行完整的内存检查
valgrind --leak-check=full -v ./a.out

它将运行您的程序并为您提供有关内存使用情况和可能泄漏的详细信息。您还应该使用以下命令编译带有调试符号的程序:

g++ -g *.h *.cc

然后追溯内存泄漏到它的原因可能很难。你可以查看这篇文章:

http://www.cprogramming.com/debugging/valgrind.html

http://valgrind.org/docs/manual/mc-manual.html#mc-manual.leaks

答案 1 :(得分:1)

Valgrind是好的,非侵入性的。

但是,如果可以的话,AddressSanitizer的减速应该会少得多。

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

答案 2 :(得分:0)

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