The short answer
On Linux, Valgrind is a popular choice for finding leaks in C++ programs. On Linux, AddressSanitizer with its LeakSanitizer part is usually faster, and it works with GCC and Clang. On Windows, Visual Studio’s debug heap can report leaks, and Dr. Memory works on Windows and Linux. Static analysis tools can point to risky code, but they can’t see what happens while the program runs. Smart pointers and standard containers prevent most leaks before you need any tool.
Valgrind
Valgrind’s Memcheck tool reports leaks when the program exits and shows the line where each leaked block was allocated. You don’t need a special build, but debug information makes the report readable. A basic run looks like this:
valgrind --leak-check=full ./program
Valgrind is built for Linux and doesn’t run natively on Windows. It is also slow, so it suits test runs best. For setup and reading the output, how to use valgrind in c walks through each step.
AddressSanitizer and LeakSanitizer
AddressSanitizer (ASan) is built into GCC and Clang. You turn it on at compile time, and it checks memory accesses while the program runs:
g++ -g -O1 -fsanitize=address -fno-omit-frame-pointer -o leak leak.cpp
./leak
On Linux, LeakSanitizer is included with ASan and checks for leaks when the program exits. If it finds any, it prints a report with the stack trace for each allocation, with lines such as Direct leak of 20 byte(s). ASan also catches out-of-bounds access, use after free, and double free, which overlaps with Valgrind’s checks. It usually slows programs less than Valgrind does. Don’t combine ASan and Valgrind in the same build.
Visual Studio’s debug heap on Windows
With MSVC, debug builds can report leaks through the C runtime’s debug heap. Include <crtdbg.h> and call _CrtDumpMemoryLeaks() at the end of main to print the allocations that were never freed. The report appears in the debugger’s output window. Use it only in debug builds, since the debug heap is not part of release builds.
Dr. Memory
Dr. Memory is an open-source memory debugger for Windows and Linux. It reports leaks and uninitialized reads. It’s a good option when you want the same tool on both platforms.
Static analysis tools
clang-tidy, cppcheck, and the Clang Static Analyzer examine your source without running it. They can flag suspicious code, such as a new with no matching delete in the same function. They can’t follow every path a program takes at runtime, so they miss some leaks and sometimes report false alarms. Run them alongside a runtime tool.
Prevent leaks in the first place
Most C++ leaks come from manual new and delete calls that fall out of step, such as an early return that skips the delete. Standard types remove that bookkeeping:
std::vectorandstd::stringfree their memory when they go out of scope.std::unique_ptrowns one object and deletes it when it goes out of scope.std::shared_ptrdeletes its object after the last owner is gone. Cycles ofshared_ptrcan still leak, so usestd::weak_ptrto break them.
Create smart pointers with std::make_unique and std::make_shared. Raw malloc calls are a special case, and when should I use malloc in C covers them.
Which one to pick
- Linux, and you want detailed reports: Valgrind.
- GCC or Clang on Linux, and you want faster runs: AddressSanitizer with LeakSanitizer.
- Windows with MSVC: the debug heap, with
_CrtDumpMemoryLeaks(). - Windows and Linux with one tool: Dr. Memory.
- Any platform: smart pointers and standard containers, with a static analyzer as an extra check.