Skip to content
systemcoding.dev
Debugging

Read Memcheck Leak Reports: Definitely, Indirectly and Possibly Lost

Learn the Memcheck leak categories, definitely, indirectly, possibly lost and still reachable, with a sample program for each and a fix-first order.

7 min read
Summarize in AI
Flat illustration of heap memory as allocated and free blocks, with the Rust, Zig and C logos.

A Memcheck leak summary has five lines, and beginners often treat them all as the same bug. They are not. One line means a real leak, one is a leak that disappears when you fix another, one is a warning that may be a false alarm, and one is memory the program still held on to at exit. Knowing which is which decides what you fix and what you leave. This post uses one small C program per category with Valgrind 3.24 on Linux 6.12, shows the exact report each one produces, and ends with a table of which categories to act on. The first post of the Valgrind series, on finding memory leaks with Memcheck, shows how to run the tool and read the heap summary.

The model behind the categories

At exit, Memcheck scans memory the way a garbage collector would. It starts from the roots, which are the registers, the stacks and the global data of the program, and follows every pointer it finds into the heap. A heap block it can reach is "reachable". A block it cannot reach has been leaked, and Memcheck classifies it by how a pointer to it might still exist. The definitions are in the leak kinds section of the Memcheck manual.

CategoryWhat Memcheck foundIs it a bug?
Definitely lostNo pointer to the block existsYes, fix it
Indirectly lostThe only pointers to the block are inside definitely lost blocksYes, but fixed by freeing its parent
Possibly lostOnly an interior pointer to the block existsOften yes; check
Still reachableA pointer to the start of the block exists at exitUsually not

Run all examples with --leak-check=full --show-leak-kinds=all, because the default output hides the last two.

Definitely lost

The block's address is gone. This is the plain leak:

lost.cc
#include <stdlib.h>

int main(void)
{
    char *p = malloc(100);
    p = NULL;
    return 0;
}

The only pointer was overwritten. The report:

Text
==5101== 100 bytes in 1 blocks are definitely lost in loss record 1 of 1
==5101==    at 0x4846828: malloc (vg_replace_malloc.c:381)
==5101==    by 0x10915E: main (lost.c:5)
==5101==
==5101== LEAK SUMMARY:
==5101==    definitely lost: 100 bytes in 1 blocks
==5101==    indirectly lost: 0 bytes in 0 blocks
==5101==      possibly lost: 0 bytes in 0 blocks
==5101==    still reachable: 0 bytes in 0 blocks
==5101==         suppressed: 0 bytes in 0 blocks

Fix these first. They are certain, and each stack leads to the allocation. The usual causes are overwriting a pointer before freeing it, returning from a function without freeing a local allocation, and an error path that skips cleanup.

Indirectly lost

A block is indirectly lost when the only pointers to it live inside blocks that are themselves definitely lost. The linked list from the previous post is the standard example, but a smaller one makes the idea plain:

indirect.cc
#include <stdlib.h>

struct holder {
    char *data;
};

int main(void)
{
    struct holder *h = malloc(sizeof *h);
    h->data = malloc(64);
    h = NULL;
    return 0;
}

Losing h loses the holder (8 bytes on a 64-bit system), and the 64-byte buffer that only the holder pointed to goes with it:

Text
==5152== 72 (8 direct, 64 indirect) bytes in 1 blocks are definitely lost in loss record 1 of 1
==5152==    at 0x4846828: malloc (vg_replace_malloc.c:381)
==5152==    by 0x10917E: main (indirect.c:9)
==5152==
==5152== LEAK SUMMARY:
==5152==    definitely lost: 8 bytes in 1 blocks
==5152==    indirectly lost: 64 bytes in 1 blocks

The record shows one stack, the direct block's allocation, and the 64 indirect bytes are folded into its total. Do not hunt for the second allocation separately. Free the holder and its buffer properly, and both lines disappear. The practical lesson is to read the definitely lost records first, because they explain the indirect numbers, and the indirect counts show how much memory one missing free is costing.

Possibly lost

"Possibly lost" is the category people misread most. Memcheck found a pointer that points into the middle of the block, an interior pointer, but none that points to the start. The block might be leaked, or the program might be holding an interior pointer on purpose and computing the start later. For example:

possible.cc
#include <stdlib.h>

int main(void)
{
    char *p = malloc(100);
    p = p + 10;
    return 0;
}

Now p points ten bytes into the block, and the start address is nowhere in memory:

Text
==5203== 100 bytes in 1 blocks are possibly lost in loss record 1 of 1
==5203==    at 0x4846828: malloc (vg_replace_malloc.c:381)
==5203==    by 0x10915E: main (possible.c:5)

This is a real bug in the sample, because free(p) on the shifted pointer would be invalid. The common legitimate cases are different: C++ standard library containers and some string implementations store pointers inside blocks, and custom allocators or strtok-style parsers advance a cursor. If you meant to keep the interior pointer, either keep the base pointer too, or, for known C++ library layouts, ask Memcheck to recognize them with --leak-check-heuristics=all. Hiding the category with --show-possibly-lost=no removes the noise but also removes the warning. The better fix in new code is to keep the base pointer in a separate variable and derive the cursor from it.

In practice, treat "possibly lost" as a lead, not a verdict. Check whether the stack leads to code you own, then check whether the pointer arithmetic is intentional.

Still reachable

The program ended while it still had a pointer to the start of the block. Nothing is lost in the sense that you could still free it. The usual case is a global:

reachable.cc
#include <stdlib.h>

static char *cache;

int main(void)
{
    cache = malloc(256);
    return 0;
}
Text
==5254== 256 bytes in 1 blocks are still reachable in loss record 1 of 1
==5254==    at 0x4846828: malloc (vg_replace_malloc.c:381)
==5254==    by 0x10915E: main (reachable.c:7)
==5254==
==5254== LEAK SUMMARY:
==5254==    definitely lost: 0 bytes in 0 blocks
==5254==    indirectly lost: 0 bytes in 0 blocks
==5254==      possibly lost: 0 bytes in 0 blocks
==5254==    still reachable: 256 bytes in 1 blocks

The operating system returns the memory when the process exits, so this is harmless for a short-lived program. It matters for two reasons. In a library, "still reachable" memory at unload is a leak for every caller that loads and unloads it. And a test that checks for leaks wants a clean exit, so freeing global state in an explicit shutdown function keeps the signal clean. Many libraries, such as glibc internals, leave a few reachable blocks on purpose; those are what suppression files hide.

What to fix first

Use the categories as a triage order.

  1. Definitely lost. Fix every record. They are the ones that grow with load.

  2. Possibly lost. Open the stack. Decide if the interior pointer is deliberate.

  3. Indirectly lost. No work of its own; it clears when the parent is freed.

  4. Still reachable. Free at shutdown if you want a clean check, but do not delay a release for it.

The loss record numbering, loss record 3 of 7, orders records by size within a category, so the first record of "definitely lost" is the largest leak. In CI, --errors-for-leak-kinds=definite,possible counts those two as errors and ignores the rest, a sensible default for most projects.

A worked triage of a mixed report

Real reports mix the categories, and the order you read them in saves time. Suppose a summary reads 4,096 bytes definitely lost in 3 blocks, 1,200 bytes indirectly lost in 40 blocks, 64 bytes possibly lost in 1 block and 9,000 bytes still reachable in 12 blocks. Start with the three definite records. If one of them is labeled (112 direct, 1,100 indirect), that single missing free explains most of the indirect total, since a container such as a tree or a list was dropped with its elements attached. Fix that one and rerun before touching anything else; the indirect number usually collapses.

Next look at the single possibly lost block. Open its stack and find the code that moves the pointer. A parser that walks a buffer with p++ and never resets it is the classic case, and the fix is a separate cursor variable. Last, skim the still reachable blocks for ones allocated in your own files. A cache or registry you fill at start-up and never empty is a design choice, but a block allocated per request that stays reachable through a growing global list is a slow leak that Memcheck calls reachable and a production server calls an outage. The size of the list across two runs of different length tells you which it is: if the reachable total grows with the input, it is a leak in practice even though no pointer was lost.

Finally, rerun after each fix, because blocks reclassify. A block that was indirectly lost can turn into definitely lost once its parent is found and freed incorrectly, and a possibly lost block can become still reachable when you stop shifting its pointer.

Share

Keep reading

Get new posts by email

We send one email when a new post is published. No spam.

Unsubscribe in one click. No spam, ever.