4

Can anyone give me any simple syntax for running OSX's leaks tool for detecting memory leaks in a compiled C program? I'm on 10.8, so I'm running into serious compatibility issues with valgrind.

Most of the stuff I've read about XCode's Leaks/Instruments involves being in an XCode environment -- not something I want to do for my C programs.

Is there a way I can just run leaks on a compiled C program from the command line? If not, are there any other reliable alternatives I can use while waiting for an updated version of valgrind, or do I have to set up a VM with a Linux distro?

user1427661
  • 11,158
  • 28
  • 90
  • 132
  • Set up a Linux distro in a VM anyway, it's not much setting up (install VM, download pre-made virtual disk(s) with Linux(es) of your choice (my choice is usually Lubuntu), boot VM to Linux desktop). Such a thing comes in handy quite often... – hyde Apr 01 '13 at 16:22
  • Of course... How much of the google searches for "/usr/bin/leaks" and "/usr/bin/malloc_history" have you read? – autistic Apr 01 '13 at 16:22
  • As KristianSpangsege said, I tried using `leaks`, I didn't have the `iprofiler`, and experienced the same bad behavior, see my [answer](https://stackoverflow.com/a/56940871/2411320) for more. Maybe I don't use it correctly, who knows, the man page didn't help. – gsamaras Jul 08 '19 at 18:47
  • Similar q/a: https://stackoverflow.com/a/67443340/3160967 – mwag Apr 27 '23 at 06:05

2 Answers2

5

Use the iprofiler tool (manpage) by adding this to the start of your command line:

iprofiler -leaks -d $HOME/tmp

(where $HOME/tmp is where you want the results written).

You can then open up the resulting .dtps bundle using Instruments to check for leaks (or any of the other 4 checks that iprofiler performs).

If you are using clang then compile with both -O3 and -g (as clang doesn't support -pg).

trojanfoe
  • 120,358
  • 21
  • 212
  • 242
0

This seems to work (macOS Catlina 10.15.6):

cd /usr/local/lib
ln -s $(xcode-select -p)/usr/lib/libLeaksAtExit.dylib
leaks --atExit -- ./a.out

Best to remove the symlink when done.

References:

Manav
  • 10,094
  • 6
  • 44
  • 51