Valgrind and Memory Leaks

What Tools Can I Use to Detect Memory Leaks?

Compare Valgrind, AddressSanitizer, LeakSanitizer, Dr. Memory and Visual Studio's debug heap for finding memory leaks in C++, and when to use each.

On this page
  1. The short answer
  2. Valgrind
  3. AddressSanitizer and LeakSanitizer
  4. Visual Studio’s debug heap on Windows
  5. Dr. Memory
  6. Static analysis tools
  7. Prevent leaks in the first place
  8. Which one to pick

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::vector and std::string free their memory when they go out of scope.
  • std::unique_ptr owns one object and deletes it when it goes out of scope.
  • std::shared_ptr deletes its object after the last owner is gone. Cycles of shared_ptr can still leak, so use std::weak_ptr to 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.

The complete guide to valgrind and memory leaks

How to Use Valgrind With C++
Read the guide

Keep exploring

Guides from other topics

All topics →

Enums in C++ With Examples

Learn enums in C++ with clear examples: syntax, automatic values, enum class, switch statements, underlying types, and common mistakes to avoid.

Enums 5 min read