c++ Memory Leak Detection on Mac OSX

15,653

Solution 1

Valgrind can run your program and do a full memory check with

valgrind --leak-check=full -v ./a.out

It will run your program and give you detail about the memory usage and possible leaks. Also you should compile your program with debugging symbols using :

g++ -g *.h *.cc

Then trace back a memory leak to it's cause can be hard. You can check this article :

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

or

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

Solution 2

Valgrind is ok and non-intrusive.

But if you can, the slowdown should be far less with AddressSanitizer.

It is included in g++-4.8 and clang, don't remember which version. You have to compile with the flag -fsanitize=address.

Share:
15,653
lufthansa747
Author by

lufthansa747

Updated on June 24, 2022

Comments

  • lufthansa747
    lufthansa747 almost 2 years

    I am writing code in c++ using text wrangler to write the code and compiling it using g++ from the command line. I would like to be able to detect if my code has any memory leaks. I have tried using valgrind but it does not seem to work properly for mavericks, i tried running "leak a.out" where a.out is the result of running "g++ *.h *.cc" but i get error message "leaks cannot find a process you have access to which has a name like 'a.out'" any ideas how to do a memory leak check?

    this is the output

    ==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.
    
  • lufthansa747
    lufthansa747 over 10 years
    when i do this it does not give any meaningful output that says weather or not there is a leak
  • Rémi Benoit
    Rémi Benoit over 10 years
    If you post the output, we can help you understand it. Or try the other tools proposed.
  • lufthansa747
    lufthansa747 over 10 years
    ==867== WARNING: Support on MacOS 10.8 is experimental and mostly broken. ==867== WARNING: Expect incorrect results, assertions and crashes. ==867== WARNING: In particular, Memcheck on 32-bit programs will fail t 0x39f000) --867-- reading syms from primary file (573 1) UNKNOWN fcntl 61!--867-- /usr/lib/system/libsystem_malloc.dylib (rx at 0x3a9000, rw at 0x3c5000) --867-- reading syms from primary file (102 199) UNKNOWN fcntl 61!--867-- /usr/lib/system/libsystem_network.dylib (rx at 0x3ce000, rw at 0x3f6000)
  • lufthansa747
    lufthansa747 over 10 years
    thats not even some of it, its just line after line of nonsense
  • Rémi Benoit
    Rémi Benoit over 10 years
    Please post the full log, if you don't understand a good place to start is the Valgrind faq. Although I didn't try the other solutions, I believe none of them are magic. Finding leaks can be a complicated matter, hence the many articles tackling the problem.
  • Jeremy Friesner
    Jeremy Friesner over 10 years
    One thing to note: when using valgrind to check for memory leaks, be sure your process exits naturally. If you force-quit the process (e.g. via control-c or the "kill" command, you'll get a lot of false-positives as the process never got a chance to free any of its memory before exiting)
  • lufthansa747
    lufthansa747 over 10 years
    My program exits naturally
  • Glider
    Glider almost 10 years
    AddressSanitizer does not detect leaks on Mac OS X yet. Stay tuned.
  • Glider
    Glider over 9 years
    No, not even in Clang. I've some patch that sort of works, but it needs polishing. Sorry for late response.
  • Franklin Yu
    Franklin Yu over 7 years
    Valgrind may report false positive in OS X, see this and this.
  • Ghita
    Ghita over 6 years
    @lufthansa747 same for me. Manually running iprofiler and also the mac app instruments leaks I cannot see obvious leaks...