I’ve been trying to chase down an annoying bug that I suspected to be a case of using uninitialized memory. The problem is, it only shows up about 1 in 30 times (I was lucky to notice it in the first place), and never in a debugger.
Fortunately I found that there’s a library on OSX that tweaks
malloc() to help you debug:
DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib MallocPreScribble=1 ./myprogram
Or, to do this in LLDB, since due to System Integrity Protection, your linker-affecting environment variables get wiped when you execute a system program:
lldb -- ./myprogram env DYLD_INSERT_LIBRARIES=/usr/lib/libgmalloc.dylib env MallocPreScribble=1 run
This triggered the bug every time, both inside and outside the debugger.
For more information about what you can do with libgmalloc, see this documentation. It only tells how to use that facility in Xcode, though, so the above instructions should help if you’re on the command line.