LeakSanitizer: detected memory leaks C/C++ Origin: Application & API Last updated 15 Oct 2026

C/C++: ==ERROR: LeakSanitizer: detected memory leaks — Direct leak of N byte(s)

At exit, LeakSanitizer found heap blocks that were allocated but never freed and are no longer reachable.

Exact message you may see LeakSanitizer: detected memory leaksDirect leak ofIndirect leak ofSUMMARY: AddressSanitizer: 24 byte(s) leaked in 1 allocation(s)definitely lost: bytes in blocks
Seen on: C/C++ GCC Clang

Meaning

The report groups leaks by allocation stack ("Direct leak" = nothing points to it; "Indirect leak" = reachable only from a leaked block). Fix the largest and most frequent first. Not every leak is a bug in short-lived tools, but in servers and loops they accumulate until bad_alloc or the OOM killer.

Common causes

  • new/malloc without matching delete/free
  • Early return or exception between allocation and free
  • Cyclic shared_ptr references (use weak_ptr for back-links)
  • Container of raw owning pointers never deleted
  • Third-party handle not closed (fopen, dlopen, library init)

⚡ Quick fix

  1. Replace raw new with std::make_unique / containers
  2. Break shared_ptr cycles with weak_ptr
  3. Run with ASAN_OPTIONS=detect_leaks=1 and fix top stacks first

Detailed fix by platform

GCC / Clang

  1. g++ -g -fsanitize=address main.cpp && ASAN_OPTIONS=detect_leaks=1 ./a.out
  2. valgrind --leak-check=full --show-leak-kinds=all ./app

macOS

  1. LeakSanitizer is not in Apple Clang's ASan; use leaks --atExit -- ./app or Instruments

Code examples

RAII instead of new/delete

cpp
#include <memory>
#include <vector>

struct Node { std::vector<std::unique_ptr<Node>> kids; };

int main() {
    auto root = std::make_unique<Node>();                // freed automatically at scope exit
    root->kids.push_back(std::make_unique<Node>());
    // Node* leak = new Node;                            // "Direct leak of 24 byte(s)"
}

How to diagnose

  1. Stack — Which allocation stack leaks the most?
  2. Ownership — Who should free it?
  3. Exit — Early returns or exceptions?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…Bus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errordouble free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…runtime error: — C/C++: runtime error: signed integer overflow / null pointer / shift exponent too large (UndefinedBehaviorSanitizer) (C/C++)runtime error:Segmentation fault — C/C++: Segmentation fault (core dumped) — SIGSEGV from a null, dangling or out-of-bounds pointer (C/C++)Segmentation faultstack overflow — C/C++: stack overflow — infinite recursion or huge local array (Segmentation fault, 0xC00000FD) (C/C++)stack overflowstd::bad_alloc — C++: terminate called after throwing an instance of 'std::bad_alloc' — what(): std::bad_alloc (C/C++)std::bad_allocLeakSanit…

Explore the map of this error Full error map →

🧠 Still stuck? Analyze your error

Paste the full message, response headers or stack trace — we'll detect the platform and point to the most likely cause.

💬 What worked for others

No tips yet — did something else fix this for you?

Share what worked for you

Tips are checked by an editor before they appear. Links are removed.