42

I used to use valgrind to detect memory leaks for my C/C++ applications on Mac OS X 10.6 (Snow Leopard) and 10.7 (Lion), but I find it's not supported on recent releases like 10.8 (Mountain Lion) and 10.9 (Mavericks) when I upgraded my OS. Is there something else like valgrind that can be installed on Mac OS X 10.9?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
hago
  • 1,700
  • 2
  • 16
  • 18
  • 3
    xcode has instruments which has a memory analyzer and leak finder. Your code needs to be in xcode and you have to find your way around the guis. – Charlie Burns Nov 01 '13 at 02:17
  • I use valgrind on a library written by c++/c codes, can instruments deal with that? – hago Nov 01 '13 at 03:55
  • 1
    Also, valgrind is not primarily a leak detector, it's an invalid access detector. There is no replacement for valgrind (certainly not static analysis anyway). The answers here might work for you but you might rephrase the question. In which case it could become off topic for the given reason. – Potatoswatter Nov 01 '13 at 04:18
  • Homebrew now has an updated formula for valgrind. `brew update` and then `brew install valgrind` should now work. – BenjaminGolder Jan 25 '14 at 04:35
  • @BenjaminGolder it's still not working – hago Jan 26 '14 at 07:56
  • @BenjaminGolder are you work on 10.9 or earlier versions? it prompts an error "configure: error: Valgrind works on Darwin 10.x and 11.x (Mac OS X 10.6/7)" while running configure. – hago Jan 27 '14 at 06:51
  • @hago it sounds like you haven't updated your homebrew formula. You should run `brew update`, which should update the formula for valgrind. You can also run `brew doctor` to see the last time you updated and anything that might be wrong with homebrew. – BenjaminGolder Jan 27 '14 at 13:33

3 Answers3

12

On 2013-11-01, the valgrind team announced Valgrind 3.9.0:

We are pleased to announce a new release of Valgrind, version 3.9.0, available from http://www.valgrind.org.

3.9.0 is a feature release with many improvements and the usual collection of bug fixes. This release adds support for MIPS64/Linux, Intel AVX2 instructions and POWER8 instructions. DFP support has been added for S390. Initial support for hardware transactional memory has been added for Intel and POWER platforms. Support for Mac OS X 10.8 (Mountain Lion) has been improved. Accuracy of Memcheck on vectorized code has been improved.

It remains to be seen whether the improved Mountain Lion support means it works OK for Mavericks. It does mention that the support is only for 64-bit code.

[...time passeth...downloads happen...hopes are raised...configuration is attempted...hopes are dashed...]

Urgh!

...
checking build system type... x86_64-apple-darwin13.0.0
checking host system type... x86_64-apple-darwin13.0.0
checking for a supported CPU... ok (x86_64)
checking for a 64-bit only build... no
checking for a 32-bit only build... no
checking for a supported OS... ok (darwin13.0.0)
checking for the kernel version... unsupported (13.0.0)
configure: error: Valgrind works on Darwin 10.x and 11.x (Mac OS X 10.6/7)

Mountain Lion is based on Darwin 12.x; Mavericks is based on Darwin 13.x. I'm not sure about the messaging in the error messages, but out of the box, Valgrind 3.9.0 does not compile on OS X Mavericks.

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
  • 1
    @jfmercer: I think that's what Mountain Lion (Mac OS X 10.8.4) would have given; are you sure you updated/upgraded? Running `uname -a` on a machine definitely running Mavericks gives: `Darwin machine-name.example.com 13.0.0 Darwin Kernel Version 13.0.0: Thu Sep 19 22:22:27 PDT 2013; root:xnu-2422.1.72~6/RELEASE_X86_64 x86_64`. And `echo $OSTYPE` yields `darwin13`. You can also see 13.0.0 in the version information detected by `configure` too. – Jonathan Leffler Nov 07 '13 at 23:11
  • Heh, I compiled the program when I was on Mountain Lion, upgraded to Mavericks, and have an executable valgrind on Mavericks. However it doesn't debug any application correctly. It crashes every time. – Martijn Courteaux Dec 03 '13 at 23:44
12

Just so you know, you may not have to wait for long to see Valgrind working on Mac 10.9 a.k.a Maverick. As there has been some success according to this bug report.

It is my opinion that there are currently (as of 15th November 2013) no Valgrind alternatives for the Mac. There are some to keep an eye on though, namely Clang which has both AddressSanitizer (works on current macs) and MemorySanitizer (macs not supported yet).

There are suggestions to use Xcode's Instruments, but I did not find it useful at all.

chutsu
  • 13,612
  • 19
  • 65
  • 86
  • Xcode also has vendor-lock in. What if you are going cross-platform with different make files, etc? – Kevin Kostlan Jun 15 '14 at 16:16
  • Valgrind is one of the best tool for C memory leaks at the moment. Clang may develop tools better than vial grind but it's not quite there yet IMO. – chutsu Jun 17 '14 at 10:14
  • There is also https://clang.llvm.org/docs/LeakSanitizer.html which can be used alongside AddressSanitizer (since MemorySanitizer, as mentioned, is not supported in macOS). – badfilms Apr 04 '20 at 20:54
7

In addition to the Instruments that @Charlie_Burns mentions above, there's also the static analyzer that can tell you about some of these things just by analyzing your code. In your Xcode project, just select "Analyze" from the "Product" menu. It invokes the clang static analyzer. I think you can use clang directly from the command line if you want, too, though I've not done that.

On the command line you also have access to the leaks command and the dtracecommand.

user1118321
  • 25,567
  • 4
  • 55
  • 86
  • does instruments has a command line version? – hago Nov 01 '13 at 03:56
  • No, as it produces lots of nice graphs and such. But you can try `dtrace`. It allows you to script some of the same sorts of things that Instruments finds. Try `man dtrace` for more info. Also, there is a `leaks` command. Again, the `man` page is your friend. – user1118321 Nov 01 '13 at 04:10
  • 2
    Instruments does have a [command line version](https://developer.apple.com/library/mac/documentation/Darwin/Reference/ManPages/man1/instruments.1.html). – Ken Thomases Nov 01 '13 at 07:03
  • Xcode Analyze is pretty cool – Francesco Pegoraro Jan 12 '18 at 14:24