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.
LeakSanitizer: detected memory leaksDirect leak ofIndirect leak ofSUMMARY: AddressSanitizer: 24 byte(s) leaked in 1 allocation(s)definitely lost: bytes in blocks 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/mallocwithout matchingdelete/free- Early return or exception between allocation and free
- Cyclic
shared_ptrreferences (useweak_ptrfor back-links) - Container of raw owning pointers never deleted
- Third-party handle not closed (
fopen,dlopen, library init)
⚡ Quick fix
- Replace raw
newwithstd::make_unique/ containers - Break
shared_ptrcycles withweak_ptr - Run with
ASAN_OPTIONS=detect_leaks=1and fix top stacks first
Detailed fix by platform
GCC / Clang
g++ -g -fsanitize=address main.cpp && ASAN_OPTIONS=detect_leaks=1 ./a.outvalgrind --leak-check=full --show-leak-kinds=all ./app
macOS
- LeakSanitizer is not in Apple Clang's ASan; use
leaks --atExit -- ./appor Instruments
Code examples
RAII instead of new/delete
#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
- Stack — Which allocation stack leaks the most?
- Ownership — Who should free it?
- 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:
Similar errors
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- AddressSanitizer: heap-use-after-free C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
- Bus error C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file
Most viewed in C/C++
- terminate called after throwing an instance of C++: terminate called after throwing an instance of 'std::out_of_range' — what(): vector::_M_range_check
- undefined reference to C/C++: undefined reference to `function' — collect2: error: ld returned 1 exit status
- expected ';' before C/C++: error: expected ';' before 'x' — missing semicolon
Other ways to find it
Where this error sits
Origin: Application & API. Errors that are often confused with or caused by this one (click to open):
🧠 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.
Report a correction or suggest an improvement
Last updated 15 Oct 2026