C++

Fix LeakSanitizer "Detected Memory Leaks" — Direct vs Indirect Leaks, Traced to the Owner

A leak report at exit is a list of allocations nobody owns anymore. LeakSanitizer sorts them into direct leaks, the roots you lost, and indirect leaks, the memory those roots were holding. Each section below takes one part of the report, shows the exact output it produces, and traces it to the fix: give the allocation an owner, and the report goes away because the leak does.

Written November 20, 2026 · Tested with GCC 13.3.0 · 4 min read

By the end of this guide
  • The first error: one allocation, no owner
  • Reading the report: direct leaks are the roots
  • Direct vs indirect: a two-node list
  • The fix: give the allocation an owner

Written November 20, 2026

Tested with GCC 13.3.0

What this guide is

ERROR: LeakSanitizer: detected memory leaks is the leak checker built into AddressSanitizer reporting, at the moment the program exits, that some heap memory was never freed and no live pointer to it remains. The program itself ran to completion: its own output printed, main returned, and only then did the report appear and the exit status change. That timing is the first thing to understand. This is not a crash at the allocation and not a fault at a bad access. It is an accounting failure found at exit, and the fix is almost never at the exit. The fix is at the allocation, where the memory should have gained an owner responsible for freeing it.

LeakSanitizer is integrated into AddressSanitizer and, on x86_64 Linux, it is enabled by default in a -fsanitize=address build: there is no separate flag to add and no call to make. The checker stays dormant until the very end of the process, then runs a leak detection phase over the heap. The examples here use C++17 and GCC 13.3.0, built with g++ -std=c++17 -fsanitize=address -g; the -g puts the file and line in each allocation stack. The report prints to standard error and the program output to standard output; the transcripts below merge the two in the order they appear. Addresses, the process ID in the == markers, and the BuildId values change from run to run. The sizes, counts, line numbers, and direct and indirect labels do not.

The first error: one allocation, no owner

The smallest reproducer allocates an array, uses it, and returns without deleting it:

C++
#include <iostream>

int main() {
    int* values = new int[4];
    values[0] = 42;
    std::cout << values[0] << std::endl;
    return 0;
}
Terminal
$ g++ -std=c++17 -fsanitize=address -g direct.cpp -o direct
$ ./direct
42

=================================================================
==37066==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7fcfe8b1c6c8 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
    #1 0x61efa747323e in main /home/hatch/workspace/slot3-verify/direct.cpp:4
    #2 0x7fcfe84a01c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7fcfe84a028a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x61efa7473164 in _start (/home/hatch/workspace/slot3-verify/direct+0x1164) (BuildId: b3ffd31cd2a3cb31f3076d584f48dfffba820df1)

SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).

The program prints 42 and finishes its work. Then the report arrives. Read it in four parts. The header names the checker and the finding: LeakSanitizer: detected memory leaks. The first section, Direct leak of 16 byte(s) in 1 object(s), gives the size and the count: new int[4] is 4 values of 4 bytes, 16 bytes, in a single allocation. The stack below it is the allocation stack, not a fault stack: frame #1 is main at line 4, the new int[4] expression, and frame #0 is the array form of operator new inside the sanitizer runtime. The last line is the total for the whole report: SUMMARY: AddressSanitizer: 16 byte(s) leaked in 1 allocation(s).

The exit status of this run is 1. A later section shows the flag that sets the status to a value of your choosing. Do not assume the documented stand-alone default: a GCC AddressSanitizer build exits with status 1 on a leak report, as this run does.

Why is this memory a leak at all, when values still held its address when main returned? Because the check runs after main returns. At that point the local variable is gone, the stack frame that held the only pointer is gone, and nothing anywhere in the process points at the array. Memory that is allocated, unfreed, and unreachable is exactly what the checker reports. A pointer that exists only in a dead stack frame does not count as reachability.

Reading the report: direct leaks are the roots

A direct leak is an allocation that no pointer reaches at all. An indirect leak is an allocation that is reachable, but only from another leaked allocation. The distinction is the whole diagnostic method, because it tells you where to fix. The indirect leaks are not separate bugs. They are the contents of the direct leaks: memory that would have been reachable, and usually freed, if the root allocation had been owned and destroyed properly. Fix the direct leaks and the indirect leaks under them normally go away with them, because the owner's cleanup frees the whole structure.

So read a report top-down with one rule: the direct sections are the work list, the indirect sections are the size of what each root is holding. A report with one direct leak of 16 bytes and a summary of 80 bytes is not five bugs. It is one lost root holding 64 more bytes. The next section builds exactly that shape.

Direct vs indirect: a two-node list

This program builds a head node, gives it a payload array, links a child node behind it, gives the child its own payload, and then returns, losing the only pointer to the head:

C++
#include <iostream>

struct Node {
    Node* next;
    int* data;
};

int main() {
    Node* head = new Node;
    head->data = new int[4];
    Node* child = new Node;
    child->data = new int[8];
    child->next = nullptr;
    head->next = child;
    head->data[0] = 1;
    child->data[0] = 2;
    std::cout << head->data[0] + child->data[0] << std::endl;
    return 0;
}
Terminal
$ g++ -std=c++17 -fsanitize=address -g indirect.cpp -o indirect
$ ./indirect
3

=================================================================
==37069==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7e8c2131a548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x5615c84f32be in main /home/hatch/workspace/slot3-verify/indirect.cpp:9
    #2 0x7e8c20c9e1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7e8c20c9e28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x5615c84f31e4 in _start (/home/hatch/workspace/slot3-verify/indirect+0x11e4) (BuildId: f6fdecc397f2b5bb14bbd6dc1e205df9999a7dd1)

Indirect leak of 32 byte(s) in 1 object(s) allocated from:
    #0 0x7e8c2131a6c8 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
    #1 0x5615c84f3314 in main /home/hatch/workspace/slot3-verify/indirect.cpp:12
    #2 0x7e8c20c9e1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7e8c20c9e28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x5615c84f31e4 in _start (/home/hatch/workspace/slot3-verify/indirect+0x11e4) (BuildId: f6fdecc397f2b5bb14bbd6dc1e205df9999a7dd1)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7e8c2131a548 in operator new(unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:95
    #1 0x5615c84f3306 in main /home/hatch/workspace/slot3-verify/indirect.cpp:11
    #2 0x7e8c20c9e1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7e8c20c9e28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x5615c84f31e4 in _start (/home/hatch/workspace/slot3-verify/indirect+0x11e4) (BuildId: f6fdecc397f2b5bb14bbd6dc1e205df9999a7dd1)

Indirect leak of 16 byte(s) in 1 object(s) allocated from:
    #0 0x7e8c2131a6c8 in operator new[](unsigned long) ../../../../src/libsanitizer/asan/asan_new_delete.cpp:98
    #1 0x5615c84f32cc in main /home/hatch/workspace/slot3-verify/indirect.cpp:10
    #2 0x7e8c20c9e1c9  (/lib/x86_64-linux-gnu/libc.so.6+0x2a1c9) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #3 0x7e8c20c9e28a in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x2a28a) (BuildId: a4a7992a8e66555c8141ab2a08a8465ff6e0ea65)
    #4 0x5615c84f31e4 in _start (/home/hatch/workspace/slot3-verify/indirect+0x11e4) (BuildId: f6fdecc397f2b5bb14bbd6dc1e205df9999a7dd1)

SUMMARY: AddressSanitizer: 80 byte(s) leaked in 4 allocation(s).

Four allocations were made, and the report has four sections, but only one of them is direct. The direct leak is the head node itself: 16 bytes from line 9, new Node. A Node holds two pointers, next and data, at 8 bytes each, so 16 bytes is the node exactly. Everything else is indirect, and each indirect stack names the line that allocated it:

  • The head payload, 16 bytes from line 10: new int[4], 4 values of 4 bytes.
  • The child node, 16 bytes from line 11: the second new Node.
  • The child payload, 32 bytes from line 12: new int[8], 8 values of 4 bytes.

The arithmetic closes, and checking it is how you know you have read the report correctly. Indirect bytes: 16 + 16 + 32 = 64. Direct bytes: 16. Total: 80 bytes, which is the SUMMARY line, in 4 allocations, which is the four new expressions in the program. The child and both payloads are indirect for one reason: at exit, the only path to any of them ran through the head node, and the head node itself was unreachable. They were not lost on their own. They were lost with their root.

That is the trace to the owner. The report is not asking for four fixes at lines 9, 10, 11, and 12. It is asking who owns the head node. In this program, nobody does: head is a raw pointer in a function that returns without deleting it, and the structure has no destructor, so even the links inside it have no cleanup attached to them. The fix gives the structure an owner whose destruction frees the whole chain.

The fix: give the allocation an owner

The fix that lasts is to make ownership part of the type, so cleanup happens wherever the owner goes out of scope and cannot be forgotten at one return path. Here the head becomes a std::unique_ptr, each payload becomes a std::unique_ptr<int[]>, and the node gains a destructor that deletes the linked child, whose own destructor then deletes its child in turn:

C++
#include <iostream>
#include <memory>

struct Node {
    Node* next = nullptr;
    std::unique_ptr<int[]> data;
    ~Node() { delete next; }
};

int main() {
    auto head = std::make_unique<Node>();
    head->data = std::make_unique<int[]>(4);
    Node* child = new Node;
    child->data = std::make_unique<int[]>(8);
    head->next = child;
    head->data[0] = 1;
    child->data[0] = 2;
    std::cout << head->data[0] + child->data[0] << std::endl;
    return 0;
}
Terminal
$ g++ -std=c++17 -fsanitize=address -g fixed.cpp -o fixed
$ ./fixed
3

The program prints 3 and exits with status 0, with no report. When main returns, the unique_ptr destroys the head node. The head destructor deletes the child, which runs the child destructor. Each node's data member is a unique_ptr, so each payload array is freed by its owner's destructor. Every allocation from the leaking version now has exactly one owner, and each owner frees its allocation exactly once. Nothing in main mentions cleanup at all, which is the point: the deletes that were easy to forget are now attached to the objects themselves.

The explicit form fixes the same program without changing the types. Delete in the reverse order of ownership: the payloads and the child before the head that points at them:

C++
    delete[] child->data;
    delete child;
    delete[] head->data;
    delete head;

That version also runs clean under the same build, prints 3, and exits with status 0. It is the right fix for a small, single-exit function and for code that cannot change its types. Its weakness is the one the report just demonstrated: every return path has to repeat the deletes in the right order, and the first path that misses one is the next leak report. The owning type does the work on every path, including the ones added later and the ones taken by an exception. Prefer the owner; use the explicit deletes where the owner cannot go.

One order rule binds both forms. An allocation is freed by its owner, once: arrays allocated with new[] are freed with delete[], single objects with delete, and a parent is freed after the members it points at have been freed, never before. Freeing the head first and the child second is a use of freed memory, and AddressSanitizer reports that as a different error with a different fix.

Still reachable is not a leak

Not every unfreed allocation is reported. If a pointer to the allocation is still live at exit, in a global, a static, or memory reachable from one, the allocation is still reachable, and the checker does not report it:

C++
#include <iostream>

int* global_values = nullptr;

int main() {
    global_values = new int[4];
    global_values[0] = 42;
    std::cout << global_values[0] << std::endl;
    return 0;
}
Terminal
$ g++ -std=c++17 -fsanitize=address -g reachable.cpp -o reachable
$ ./reachable
42

This program never deletes its array either, and it exits with status 0 and no report, because global_values still points at the array when the check runs. The memory is unfreed but not lost. A clean report does not prove a program frees its memory; it proves no allocation was unreachable at exit. A long-running program that parks allocations in globals and never frees them passes this check while its memory use grows. The checker answers the reachability question, not whether reachable memory should have been freed earlier.

Turning the check off, and suppressions

The check can be switched off for a run with detect_leaks=0 in ASAN_OPTIONS:

Terminal
$ ASAN_OPTIONS=detect_leaks=0 ./direct
42

The leaking program prints 42, produces no report, and exits with status 0. Nothing was fixed. The allocation is still lost; the run simply stopped looking. This switch has two legitimate uses: running a program whose leaks are already filed and being worked on, so other sanitizer findings stay visible, and checking whether a failure actually comes from the leak check at all. As a way to make a test suite pass, it converts a failing test into a silent leak, and the suite stays green while the bug ships.

The exit status on a leak can be set with the exitcode flag, which LeakSanitizer reads from LSAN_OPTIONS in a build like this one. Setting it changes only the status, not the report:

Command
$ LSAN_OPTIONS=exitcode=42 ./direct

That run prints the same report as the first section and exits with status 42. Use a distinct value when a test harness needs to tell a leak failure apart from a crash or a failed assertion at a glance. The same run with ASAN_OPTIONS=halt_on_error=1 printed the same report and exited with status 1: the leak check runs at exit, after the program's own work is done, so there is no later work in the program for a halt to stop.

A suppression is the narrow form of switching off: it silences the leaks whose allocation stack matches a pattern, and leaves every other leak reported. The rule form is one rule per line, leak: followed by a pattern that is matched as a substring against the symbolized stack, so a function name, a file name, or a binary name can be the pattern. This program allocates inside a named function:

C++
#include <iostream>

__attribute__((noinline)) void allocate_leak() {
    int* values = new int[4];
    values[0] = 7;
    std::cout << values[0] << std::endl;
}

int main() {
    allocate_leak();
    return 0;
}

With no suppression, it reports a direct leak of 16 bytes from allocate_leak and exits with status 1. With a one-line suppressions file and the file passed through LSAN_OPTIONS, the same binary runs like this:

Terminal
$ cat suppress.txt
leak:allocate_leak
$ g++ -std=c++17 -fsanitize=address -g suppress.cpp -o suppress
$ LSAN_OPTIONS=suppressions=suppress.txt ./suppress
7
-----------------------------------------------------
Suppressions used:
  count      bytes template
      1         16 allocate_leak
-----------------------------------------------------

The program prints 7 and exits with status 0. There is no error header and no SUMMARY line for the suppressed leak. What remains is the Suppressions used block, which lists the pattern, the count of allocations it matched, and their total bytes: 1 allocation, 16 bytes, under the template allocate_leak. That block is the audit trail. A suppressed leak is still a leak; the block is how a later reader knows it was set aside on purpose, and how much memory the decision covers.

A suppression is legitimate in a narrow set of cases: the allocation is inside a library you do not control and cannot fix, the memory is allocated once at startup and held for the life of the process by design, or the leak is already filed and the suppression names that one stack while the fix is in progress. A suppression is hiding the bug when the pattern names your own allocating function, when the pattern is broad enough to catch stacks nobody reviewed, or when it is added to make a failing run pass without a decision about the memory. The test is simple: a suppression should name a leak someone has already traced to its owner and judged. If the tracing has not happened, the suppression is not a judgment. It is the report, deleted.

The exact rule: what the checker counts as a leak

At exit, the checker scans the process roots, the globals, the stacks, and the registers, and follows every pointer it finds into the heap. An allocation with at least one pointer reaching it is still reachable and is not reported. An allocation with no pointer reaching it is a leak. Among leaks, an allocation pointed to only from another leaked allocation is indirect; an allocation pointed to from nothing at all is direct. The report groups allocations by their allocation stack, prints one section per group with its byte total and object count, and ends with the SUMMARY line totaling every leaked byte and allocation.

The check is on by default in an AddressSanitizer build on x86_64 Linux. It can be run on its own with -fsanitize=leak, which links the leak runtime without the rest of AddressSanitizer's instrumentation; GCC documents that option as affecting the linking of executables, with its run-time behavior set through LSAN_OPTIONS. In an AddressSanitizer build, detect_leaks is set through ASAN_OPTIONS, and the flags specific to the leak checker, such as suppressions and exitcode, are read from LSAN_OPTIONS. The check runs once, at exit. Memory freed before exit is never reported, memory still reachable at exit is never reported, and a leak in code that never runs is never reported, because no allocation was made.

The fixes, ranked

  • First, fix the direct leaks only. Read each direct section to its allocation line, find the object or function that should own that allocation, and give it the ownership. The indirect sections under it are expected to clear with it.
  • Second, prefer an owning type: a std::unique_ptr, a container, or a destructor that frees the members. Ownership that travels with the object cannot be skipped by a new return path.
  • Third, where the types cannot change, delete explicitly, in reverse ownership order, on every path out of the scope, arrays with delete[] and objects with delete.
  • Fourth, check the arithmetic before calling a report wrong: the direct and indirect byte totals must add up to the SUMMARY line, and the allocation count must match the allocations the code made. A mismatch means a section was misread.
  • Fifth, suppress only a leak that has been traced and judged, with a pattern as narrow as the one stack it covers, and treat detect_leaks=0 as a diagnostic switch, never as the fix.

Which fix for which cause

Cause Report line Fix
Pointer lost, no owner Direct leak Add an owner
Root lost, contents follow Indirect leak Fix the direct root
Cleanup skipped on one path Direct leak Owning type, not deletes
Global still points at it No report Not a leak; check growth
Library allocates, never frees Direct leak Narrow suppression
Check itself unwanted Any leak report detect_leaks=0, filed only

Checklist

  • Read the direct sections first: they are the lost roots, and each one names the allocation line to fix.
  • Add the byte totals up: direct plus indirect must equal the SUMMARY line before you trust your reading of the report.
  • Follow the allocation stack to the owner, not to the exit: the bug is where ownership was never assigned.
  • Give each allocation one owner: a smart pointer, a container, or a destructor that frees what the object holds.
  • Match the delete to the new: delete[] for arrays, delete for single objects, members before the object that points at them.
  • Remember that still-reachable memory is not reported: a clean run proves reachability, not that memory is freed promptly.
  • Set the leak exit status explicitly in a test suite instead of assuming a documented default from another build.
  • Suppress by one named stack, for a leak already traced and judged, and keep the Suppressions used block as the record.

Sources

  • Google, AddressSanitizerLeakSanitizer — for LeakSanitizer being integrated into AddressSanitizer and enabled by default in AddressSanitizer builds on x86_64 Linux, for the checker running at the very end of the process, for detect_leaks=0 in ASAN_OPTIONS running a program without leak detection, for the stand-alone -fsanitize=leak mode, for the exitcode, suppressions, and print_suppressions flags, and for the suppressions file form of one leak:<pattern> rule per line, substring-matched against the symbolized stack, with the Suppressions used block in the output.
  • GCC, Instrumentation Options — for -fsanitize=address enabling AddressSanitizer with its run-time behavior set through ASAN_OPTIONS, for building with -g for more meaningful output, and for -fsanitize=leak enabling LeakSanitizer at link time with its run-time behavior set through LSAN_OPTIONS.

Keep Exploring