C++
Double Free Detected in Tcache Fix
One allocation gets exactly one delete. When a second delete reaches the allocator for a chunk that is already in its per-thread cache of freed chunks, glibc prints this message and aborts instead of corrupting the heap quietly — and every shape that produces it is two owners, or one owner counted twice, for the same allocation.
Written November 26, 2026 · Tested with GCC 13.3.0 · 3 min read
- The error: the same pointer deleted twice
- Two pointers, one allocation: the aliasing shape
- A destructor and its caller both delete: the class-boundary shape
- The modern fix: std::uniqueptr owns it, and there is no delete to duplicate
Written November 26, 2026
Tested with GCC 13.3.0
What this guide is
A delete expression is only valid on two kinds of pointer: a null pointer, or a pointer to an object created by a new expression. The delete expression page states that if the pointer is anything else, the behavior is undefined. A pointer whose object has already been deleted is exactly that "anything else": after the first delete, the value in the variable is a stale copy of an address that no longer belongs to the program, and deleting it again passes that address to the deallocation function a second time.
What happens next depends on the allocator, and on a glibc system the common case is caught. The first delete returns the chunk to the tcache — the per-thread cache of freed chunks that glibc's malloc keeps so a later allocation of the same size can be served without locking. When the second delete tries to put the same chunk into that cache again, the allocator recognizes the chunk as already cached, prints free(): double free detected in tcache 2, and aborts the process. The abort is the good outcome: the alternative the check prevents is the same chunk being handed out twice to two unrelated allocations later.
The three shapes below all produce that one message, byte for byte, and all three end the same way — the process is killed by SIGABRT, reported as exit status 134. They differ only in how the second owner of the allocation was created: the same variable used twice, a copy of the pointer, and a class boundary where both sides believed the allocation was theirs to delete. The fixes differ with the shape, and the last one — giving the allocation to std::unique_ptr — removes the whole category, because there is no delete left in the program to write twice.
The error: the same pointer deleted twice
The smallest form is one variable, one allocation, two deletes:
// broken.cpp
#include <iostream>
int main() {
int* p = new int(42);
std::cout << *p << "\n";
delete p;
delete p;
return 0;
}
$ g++ -std=c++20 broken.cpp -o broken_app
$ ./broken_app
free(): double free detected in tcache 2
The process aborts at the second delete and exits with status 134. Nothing in the program prints after the message: the allocator terminates the process from inside the deallocation call, so the return 0 is never reached.
Read the message as the diagnosis it is. It names the operation (free(), which is where the deallocation function ends up), the defect (a double free), and the place the duplicate was found (the tcache). It does not name a line number, because the allocator cannot know one — the defect is in the program's ownership bookkeeping, and the allocator only sees the address arriving twice. The first delete is not the bug and neither, on its own, is the second line's syntax; the bug is that the program still held a deletable pointer to an allocation it had already given back.
The minimal repair for this exact shape is to take the pointer out of circulation the moment it is deleted:
// fixed_null.cpp
#include <iostream>
int main() {
int* p = new int(42);
std::cout << *p << "\n";
delete p;
p = nullptr;
delete p;
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 fixed_null.cpp -o fixed_null_app
$ ./fixed_null_app
42
done
The program prints 42 and done and exits with status 0. The second delete is still in the source, and it is now harmless: if the pointer given to a delete expression is a null pointer, no destructors are called, and the default deallocation functions are guaranteed to do nothing when passed a null pointer. Setting the pointer to nullptr immediately after delete turns every later stray delete of that variable into a no-op. It is a real fix for this shape and a useful habit everywhere, but it protects only the variable that was nulled — the next shape is the one it cannot save.
Two pointers, one allocation: the aliasing shape
Copying a pointer copies the address, not the object. After int* q = p;, the program holds two variables that both look like owners, and deleting through both is the same double free with an extra step:
// alias.cpp
#include <iostream>
int main() {
int* p = new int(42);
int* q = p;
std::cout << *q << "\n";
delete p;
delete q;
return 0;
}
$ g++ -std=c++20 alias.cpp -o alias_app
$ ./alias_app
free(): double free detected in tcache 2
The process aborts at delete q; and exits with status 134 — the same message, because from the allocator's side nothing has changed: one address arrived at free() twice. Note what nulling does and does not do here. Writing p = nullptr; after delete p; leaves q untouched; q still holds the stale address, and delete q; still aborts. The two variables are independent copies, and no assignment to one of them reaches the other.
The fix for this shape is a rule, not a line: exactly one of the pointers is the owner, and only the owner deletes. Every other pointer to the same allocation is an observer — it may read and write through the pointer while the owner keeps the allocation alive, and it never deletes:
// alias_fixed.cpp
#include <iostream>
int main() {
int* p = new int(42);
int* q = p;
std::cout << *q << "\n";
delete p;
p = nullptr;
q = nullptr;
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 alias_fixed.cpp -o alias_fixed_app
$ ./alias_fixed_app
42
done
The program prints 42 and done and exits with status 0. There is one delete in the program, issued by the owner; both variables are then nulled, the owner because it was deleted through, the observer because its target is gone and any later use of it should fail visibly as a null dereference rather than quietly as a use of freed memory. In a larger program the observer is usually a function parameter, a member of another object, or a cached pointer in a second container — the shape to search for when this message appears is not two delete lines near each other, but two places that each believe they own the same allocation.
A destructor and its caller both delete: the class-boundary shape
The same defect hides across a class boundary, where each side's code looks correct in isolation. Here the class deletes its member in its destructor — a normal owning-class pattern — and the caller, treating the member as its own to clean up, deletes it first:
// owner.cpp
#include <iostream>
struct Buffer {
int* data;
explicit Buffer(int value) : data(new int(value)) {}
~Buffer() { delete data; }
};
int main() {
Buffer b(42);
std::cout << *b.data << "\n";
delete b.data;
return 0;
}
$ g++ -std=c++20 owner.cpp -o owner_app
$ ./owner_app
free(): double free detected in tcache 2
The process aborts and exits with status 134. The caller's delete b.data; is the first free and succeeds; the second free happens after main's last line, when b goes out of scope and ~Buffer() deletes the same address again. This is why the message in this shape appears at a moment when no delete is visibly running — the stack at the abort points into a destructor invoked by scope exit, which reads as unrelated to the line that caused it.
The fix is to pick the owner and delete the other side's delete. A class whose destructor deletes a member is declaring itself the owner; the caller's correct move is to not delete at all:
// owner_fixed.cpp
#include <iostream>
struct Buffer {
int* data;
explicit Buffer(int value) : data(new int(value)) {}
~Buffer() { delete data; }
Buffer(const Buffer&) = delete;
Buffer& operator=(const Buffer&) = delete;
};
int main() {
Buffer b(42);
std::cout << *b.data << "\n";
return 0;
}
$ g++ -std=c++20 owner_fixed.cpp -o owner_fixed_app
$ ./owner_fixed_app
42
The program prints 42 and exits with status 0: the destructor's single delete, at scope exit, is the only one. The two deleted copy operations are part of the same fix, not decoration. A copyable owning class manufactures the aliasing shape from the previous section automatically — copy the Buffer and both copies' destructors delete the same member — so an owning class either deletes its copies, as here, or implements copying as a deep copy that gives each object its own allocation. A raw owning pointer plus a compiler-generated copy is this error waiting for its first copy.
The modern fix: std::unique_ptr owns it, and there is no delete to duplicate
Every fix above is a discipline the programmer has to keep. std::unique_ptr turns the discipline into the type. A std::unique_ptr owns and manages another object through a pointer and disposes of that object when the unique_ptr itself goes out of scope; the default deleter uses the delete operator, and the class is move-only — it satisfies the requirements of MoveConstructible and MoveAssignable, but of neither CopyConstructible nor CopyAssignable — so the aliasing shape cannot be built by copying it. std::make_unique constructs the object and wraps it in a unique_ptr in one expression, so no raw owning pointer exists in the program at all:
// unique.cpp
#include <iostream>
#include <memory>
int main() {
auto p = std::make_unique<int>(42);
int* raw = p.get();
std::cout << *raw << "\n";
std::cout << "done\n";
return 0;
}
$ g++ -std=c++20 unique.cpp -o unique_app
$ ./unique_app
42
done
The program prints 42 and done and exits with status 0. There is no delete anywhere in it: p's destructor, at scope exit, performs the one deallocation. Other code that needs to look at the object borrows the observer pointer from get(), as this program does, and an observer obtained that way carries no ownership and nothing to delete — the aliasing shape's q with the dangerous half removed. The class-boundary shape collapses the same way: a member declared as a std::unique_ptr is destroyed with its object, exactly once, and a caller has no member pointer it could plausibly delete. Where the allocation is an array or a container's storage, the same principle points one step further: a std::vector or another owning container holds the elements and releases them itself, and code that never writes new or delete cannot write either of them twice.
What the tcache is, and why the abort happens there
The glibc manual describes the tcache through its memory allocation tunables: glibc.malloc.tcache_count is the maximum number of chunks of each size to cache in the per-thread cache, with a default of 7, and glibc.malloc.tcache_max is the maximum size of a request that may be met via that cache. The glibc project's MallocInternals page describes the structure behind those tunables: each thread has a per-thread cache containing a small collection of chunks that can be accessed without locking an arena, stored as an array of singly-linked lists with one size of chunk per bin.
That is enough to read the error message literally. A small allocation, freed, goes into the calling thread's tcache bin for its size. Freeing the same chunk again means inserting a chunk that the bin already contains — and a singly-linked list that gains the same node twice is corrupted, which is why the allocator checks for exactly this case and terminates the process when it finds it, rather than letting the corrupted list hand the chunk out twice later. Two consequences follow for debugging. First, the message is specific to allocations small enough to be tcache-managed. The same defect on a 200,000-byte array — large enough to be served by mmap, outside the tcache — prints nothing at all and dies differently:
// big.cpp
#include <iostream>
int main() {
char* p = new char[200000];
p[0] = 'x';
std::cout << p[0] << "\n";
delete[] p;
delete[] p;
return 0;
}
$ g++ -std=c++20 big.cpp -o big_app
$ ./big_app
That program produces no output and no allocator message, and is killed by SIGSEGV with exit status 139. The absence of the tcache message therefore never proves the absence of a double free; it only shows the allocation did not pass through the tcache check. Second, the abort site is the second free, which in the class-boundary shape is a destructor at scope exit — start the search from "who else owned this address," not from the line the debugger stops on.
The exact rule: one allocation, one owner, one delete
The delete expression page gives the validity condition and the null exception in one place. The pointer operand must be a null pointer, a pointer to a non-array object created by a new-expression, or a pointer to a base subobject of such an object; if it is anything else, the behavior is undefined — and a pointer to an already-deleted object is not a pointer to an object created by new anymore, because that object was destroyed by the first delete. If the pointer is a null pointer value, no destructors are called, and the default deallocation functions are guaranteed to do nothing when passed a null pointer — which is why nulling after delete converts any later delete of that variable into a guaranteed no-op, and why it cannot help a second variable that still holds the old address.
The ownership half of the rule is what std::unique_ptr encodes: it owns and manages the object, disposes of it when the unique_ptr goes out of scope or is assigned another pointer, and cannot be copied, so ownership can be transferred by a move — visibly, in the source — but never silently duplicated. Put together, the two statements predict every shape in this guide. One variable deleted twice is an invalid operand caught by the allocator. Two variables aliasing one allocation are two claimed owners for one object, and nulling one leaves the other's claim intact. A destructor and a caller deleting the same member are the same two claims split across a class boundary. And a unique_ptr leaves exactly one claim, held by a type whose destructor is the only delete.
The fixes, ranked
- First, for new code and for any allocation you are touching anyway: own it with
std::unique_ptrcreated bystd::make_unique, and borrow raw pointers fromget()only as non-owning observers. This removes the defect class — there is nodeletein your code to duplicate, and the owner cannot be copied. - Second, for an owning class: let the destructor be the only delete, delete the class's copy operations (or implement them as a deep copy), and remove any delete of the member at call sites. One owner, stated in the type.
- Third, for plain raw-pointer code that stays raw: exactly one designated owner deletes, every other pointer to the allocation is an observer that never deletes, and the owner nulls its pointer immediately after deleting.
- Fourth, as a habit everywhere raw pointers are deleted:
delete p; p = nullptr;— it makes a repeated delete of the same variable a guaranteed no-op. Know its limit: it does nothing for a second variable holding the same address. - Fifth, do not silence the abort by removing one of the deletes at random, and do not treat the message as an allocator false positive. Both deletes express an ownership claim; removing the wrong one converts the abort into a leak or a use-after-free elsewhere. Establish which side owns the allocation, then remove the non-owner's claim.
Which fix for which cause
| Cause | Message | Fix |
|---|---|---|
| Same variable deleted twice | free(): double free detected in tcache 2; abort, exit status 134 |
Null the pointer right after delete, or remove the second delete |
| Two pointers alias one allocation, both deleted | Same message; abort, exit status 134 | One owner deletes; the other pointer is an observer only |
| Destructor deletes a member the caller also deleted | Same message, raised at scope exit | The class owns it; remove the caller's delete |
| Copyable class with a raw owning member | Same message when a copy is destroyed | Delete the copy operations, or deep-copy |
| Any new allocation | No error yet | std::make_unique / std::unique_ptr; no raw delete |
| Large (200,000-byte) allocation freed twice | No message; killed by SIGSEGV, exit status 139 |
Same ownership fixes; the message's absence proves nothing |
| ## Checklist |
- Read the message as three facts: the operation was
free(), the defect is a double free, and the duplicate was found in the per-thread cache. The bug is an ownership count, not a syntax error. - Count the owners of the address, not the
deletelines near the abort. Search for copies of the pointer, members pointing at the same allocation, and containers holding a second pointer to it. - Check destructors when the abort fires at scope exit. A member deleted by both its class and its caller aborts inside the destructor, away from the line that caused it.
- Check copyability on any class with a raw owning member. A compiler-generated copy duplicates the ownership claim, and both copies' destructors will delete.
- Apply the ownership fix, not just a deletion fix: one owner deletes, observers never do, and the owner nulls its pointer after deleting.
- Prefer removing the raw pointer entirely:
std::unique_ptrfor a single object, an owning container for a collection. Code with nodeletecannot double-delete. - After the fix, confirm the program completes the work the abort was cutting short: the fixed programs here print their value and their completion line and exit with status 0.
Related guides
- Fix a Dangling std::string_view — Lifetime Rules for Views and References to Temporaries — the neighboring lifetime defect: a view that outlives the object it points at, instead of an object deleted by two owners.
- Fix C++ shared_ptr Cycle Leak — the opposite ownership failure: two owners keeping an allocation alive forever, and
std::weak_ptras the non-owning link. - Fix LeakSanitizer "Detected Memory Leaks" — allocations nobody owns anymore, traced to the owner that should have released them.
Sources
- cppreference, Delete expression — for the pointer operand of a delete expression having to be a null pointer or a pointer to an object created by a new-expression, for any other pointer making the behavior undefined, and for a null pointer value meaning no destructors are called and the default deallocation functions being guaranteed to do nothing when passed a null pointer.
- cppreference, std::unique_ptr — for
unique_ptrowning and managing an object and disposing of it when theunique_ptrgoes out of scope, for the default deleter using thedeleteoperator, and forunique_ptrbeing move-only: MoveConstructible and MoveAssignable, but neither CopyConstructible nor CopyAssignable. - cppreference, std::make_unique — for
make_uniqueconstructing an object of the given type and wrapping it in astd::unique_ptr, equivalent tounique_ptr<T>(new T(...)). - The GNU C Library Manual, Memory Allocation Tunables — for the tcache being a per-thread cache, for
glibc.malloc.tcache_countas the maximum number of chunks of each size cached in it with a default of 7, and forglibc.malloc.tcache_maxas the maximum request size that may be met via the per-thread cache. - glibc Wiki, MallocInternals — for each thread having a per-thread cache (tcache) of chunks accessible without locking an arena, stored as an array of singly-linked lists with one chunk size per bin.
Keep Exploring
Seven tips and seven puzzles so far, each one a short runnable example with the fix explained.
Paste an AddressSanitizer, UBSan, or ThreadSanitizer report and get the bug, the file and line, and the fix \u2014 in plain language.
C++20 and C++23 features, migration paths, and measured trade-offs.
The full C++ hub \u2014 language basics, the standard library, coroutines, and measured comparisons.
Paste a [Service] section and get a plain-language verdict on every sandboxing setting it finds.