The malloc library provides debugging features to help you track down memory smashing bugs, heap corruption, references to freed memory, and buffer overruns.
You enable these debugging options through a set of environment variables. With the exception of
MallocCheckHeapStart
and MallocCheckHeapEach
, the value for most of these environment variables is ignored.
To disable a variable from Terminal, use the unset
command.
Table 1 lists some of the key environment variables and describes their basic function. For a complete list of variables, see the
malloc
man page.The following example enables stack logging and heap checking in the current shell before running an application. The value for
MallocCheckHeapStart
is set to 1 but is irrelevant and can be set to any value you want.You could also set these variables from your shell’s startup file, although if you do be sure to
export
each variable.% MallocStackLogging=1 |
% MallocCheckHeapStart=1000 |
% MallocCheckHeapEach=100 |
% ./my_tool |
If you want to run your program in
gdb
, you can set environment variables from the Xcode debugging console using the command set env
, as shown in the following example:% gdb |
(gdb) set env MallocStackLogging 1 |
(gdb) run |
Some of the performance tools require these options to be set in order to gather their data. For example, the
malloc_history
tool can identify the allocation site of specific blocks if the MallocStackLogging
flag is set. This tool can also describe the blocks previously allocated at an address if the MallocStackLoggingNoCompact
environment variable is set.The
leaks
command line tool will name the allocation site of a leaked buffer if MallocStackLogging
is set.