Segmentation faults and other memory bugs (reading uninitialized
memory, reading/writing beyond the bounds of an array, memory leaks,
etc.) can be hard to track down with a traditional debugger. Memory
errors can be elusive, and may not cause the program to crash
immediately. A program with memory errors may may even appear to work
correctly on some datasets or on some machines.
We recommend using a special debugger to find memory errors, for
example Valgrind or Dr. Memory. Commercial
versions of these tools include Purify and
only works on Unix-based systems (e.g., Linux, FreeBSD, and
MacOSX). It does not work on Cygwin because Cygwin emulates
UNIX at the library layer, but Valgrind operates at the system call
layer and the Windows system calls are significantly different than
UNIX system calls..
To use Valgrind...
Note: Because the STL string class uses its own allocator,
there may be a warning about memory that is ``still reachable'' even
though you've deleted all your dynamically allocated memory. The
newest version of Valgrind automatically suppresses this specific
error, so you will see this listed as a ``suppressed leak''.
- Your program should be compiled with debug information enabled
by specifying the -g flag:
g++ -g main.cpp foo.cpp -o foo.out
- Then run the program by adding Valgrind to the
beginning of your command line (replace foo.out arg1 arg2
with your program name and arguments):
valgrind --leak-check=full --show-reachable=yes foo.out arg1 arg2
Those of you using Windows and the Microsoft Visual Studio compiler
which detects the
same classes of errors as Valgrind.
- Obtain Dr. Memory. To easily place it on the system path, use the
installer (the .exe file). Alternately, you can instead obtain the
.zip file for a local install.
- Run the installer. Select ``Add to system path for current user''.
- Build your application as 32-bit with Visual Studio (32-bit is
the default). Be sure to include debug information. You can verify
that you are including debug information by looking at the
properties of your build target (this example is for Visual Studio
Press Alt-F7 to bring up the configuration properties. Under
"Configuration Properties | C/C++ | General", the "Debug
Information Format" entry should say "Program Database (/Zi)".
Also, turn off the Debug version of C Library:
Under "Configuration Properties | C/C++ | Code Generation", change
"Runtime Library" to be "Multi-threaded DLL (/MD)".
Also, turn off the Debug version of C++ Library:
Under "Configuration Properties | C/C++ | Preprocessor", remove
"_DEBUG" from the list of "Preprocessor Definitions". It probably says
"WIN32;_DEBUG;_CONSOLE" by default. Change it to "WIN32;_CONSOLE".
- Open a cmd shell. Hit the Windows key and type cmd and
press enter. This is the black command-line window. Note: this is
not the Cygwin shell.
- Change to the directory containing your application executable.
E.g., cd \projects\hw1\Debug
- Run this command, replacing foo.exe arg1 arg2
with your executable name and any command line arguments:
drmemory -brief -batch -- foo.exe arg1 arg2
If you don't see any extra output from Dr. Memory as your program
runs, remove the -batch flag and the Dr. Memory output will be sent to
a file and notepad will launch automatically to display this file.
drmemory -brief -- foo.exe arg1 arg2
- Dr. Memory will report errors to the screen as it runs. It will print a
summary at the end of what it found.
- For questions, bug reports, and discussion, use the
Dr. Memory Users group:
Note on compiling with the Visual Studio Compiler
To build an executable from a set of .cpp files using the Visual Studio
compiler but without using the Visual Studio IDE:
- Launch the Visual Studio Command Prompt. From the Start menu,
under All Programs, find your Visual Studio version (e.g., 2008) and
expand it. Then expand Visual Studio Tools. Select the "Visual
Studio 2008 Command Prompt". (You don't want the x64 or Cross Tools
- At the command line, change to the directory containing your
- Run the compiler, which is called "cl". This will build
hw.exe from all .cpp files in the current directory:
cl /Zi /EHsc /Fehw.exe *.cpp
If you installed Dr. Memory before you opened the Command Prompt, you
can run drmemory from the same prompt.
This Command Prompt is a cmd shell in which a batch file that
comes with Visual Studio has been executed. This batch file is called
vcvars.bat and it sets up the path and environment variables
needed to run the compiler from the command line.
You can extract the environment variables from the batch file and set them
up in your .bashrc so you can build from a Cygwin shell.