A new treat in Xcode 1.2
Apple recently released Xcode 1.2 with the standard assortment of bug fixes and new features. One new feature is a bit hidden, though, and since I’ve only seen it mentioned elsewhere once, I figure it’s high time I let some other folks know. It’s called libgmalloc, or Guard Malloc if you’re into marketing terms.
libgmalloc is the best tool in existence for finding memory access bugs on Mac OS X. It’ll track down references to uninitialized memory, reading off the end of an array, accesses to freed memory, and so on. There’s a catch, though. When you run an application with libgmalloc, it’ll run significantly slower — perhaps two orders of magnitude slower — and it’ll use far more memory than it otherwise would. If you have a very fast system with tons of memory, you might be able to run various applications with it on a regular basis; if not, you might just test your application every so often.
First, though, you have to install libgmalloc. It’s a bit hidden. On the Xcode 1.2 Tools CD, you’ll find a Preview folder at the root level. Open that up and install the April2004LibgmallocPreview package. (No, I don’t know why there aren’t any spaces in the package name.)
Once it’s installed, running it is simple if you’re comfortable with the command line. Unfortunately, there’s no integration with Xcode yet. To use libgmalloc, you’ll have to set up two environment variables. I’ll use tcsh syntax here because that’s the shell I use; if you use bash, you’ll have to translate.
% setenv DYLD_FORCE_FLAT_NAMESPACE 1 % setenv DYLD_INSERT_LIBRARIES /usr/lib/libgmalloc.B.dylib
Now just run your application:
% ./myApp
and be very, very patient. If your application runs correctly (albeit slowly), congrats! You probably don’t have any memory stompers.
I frequently use libgmalloc from within gdb. That way I can debug my application immediately when it crashes due to an invalid memory access. Here’s how to do that:
% gdb myApp gdb% set env DYLD_FORCE_FLAT_NAMESPACE 1 gdb% set env DYLD_INSERT_LIBRARIES /usr/lib/libgmalloc.B.dylib gdb% run
Note that you want to set the environment variables from within gdb, rather than on the command line before you launch gdb. If you launch them before you launch gdb, you’ll be running gdb with libgmalloc, and it’ll also run a hundred times slower.
If you find crashes in Apple’s frameworks while running with libgmalloc, file bug reports! Mention that you were running with libgmalloc and the engineer who receives the bug will hopefully recognize that it’s unlikely that the crash was caused by a memory smasher in the application’s code.
Update: As of Tiger, DYLD_FORCE_FLAT_NAMESPACE is no longer required for libgmalloc. The only variable you have to set to use libgmalloc is DYLD_INSERT_LIBRARIES.
Buzz Andersen Said,
May 19, 2004 @ 9:56 am
Cool–I never would have known…
Michael Tsai's Weblog Said,
May 19, 2004 @ 1:12 pm
libgmalloc in Xcode 1.2
Eric Albert: Apple recently released Xcode 1.2 with the standard assortment of bug fixes and new features. One new feature is a bit hidden, though, and since I’ve only seen it mentioned elsewhere once, I figure it’s high time I let some other folks kno…
Joe Said,
May 19, 2004 @ 9:59 pm
Awesome - going to have to try that one out!
Anonymous & Coward Said,
May 20, 2004 @ 7:15 am
“No, I don’t know why there aren’t any spaces in the package name.”
Think 31 limitation (probably).
Ted Leung on the air Said,
May 27, 2004 @ 11:18 pm
Grist from the NNW Beta List
Some tidbits from the NetNewsWire beta list (sorry in info on the beta itself, other than it rocks)… It turns out that there’s a nice leak checker built in to Panther. If you do something like this: export MallocStackLogging=1; open -a ~/Applicat
Ted Leung on the air Said,
May 28, 2004 @ 12:15 pm
Grist from the NNW Beta List
Some tidbits from the NetNewsWire beta list (sorry no info on the beta itself, other than it rocks)… It turns out that there’s a nice leak checker built in to Panther. If you do something like this: export MallocStackLogging=1; open -a ~/Applicati
Brian Said,
July 8, 2004 @ 1:45 pm
Does libgmalloc work with g++’s new/delete memory, also?
Eric Said,
July 9, 2004 @ 12:23 am
It’s all malloc in the end, so yep, it does.
Marc Said,
January 31, 2006 @ 11:09 pm
One problem though.
Many apps don’t function correctly with DYLD_FORCE_FLAT_NAMESPACE because they rely on two level namespaces.
http://lists.apple.com/archives/xcode-users/2004/Dec/msg00027.html