C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free
The program read or wrote a heap block after it had been freed; ASan shows who freed it and where it was allocated.
AddressSanitizer: heap-use-after-freeheap-use-after-free on addressfreed by thread T0 herepreviously allocated by thread T0 hereAddressSanitizer: stack-use-after-scope Meaning
Use-after-free is a leading cause of security bugs. The report has three stacks: the bad access, the freed by thread T0 here stack, and the previously allocated by stack. Typical cases are iterators or references into a std::vector that reallocated, a raw pointer outliving its owner, and callbacks capturing this of a deleted object.
Common causes
- Using a pointer after
delete/free - Holding a reference, pointer or iterator into a container that was resized or erased
- Callback or thread using an object that has been destroyed
- Returning
.c_str()/.data()of a temporary string - Two owners where one deletes
⚡ Quick fix
- Compare the access stack with the "freed by" stack
- Replace raw owning pointers with
unique_ptr/shared_ptr - Don't keep references or iterators across insertions
Detailed fix by platform
GCC / Clang
- Build with
-fsanitize=address -gand run the failing case; setASAN_OPTIONS=detect_stack_use_after_return=1for stack variants
Linux
valgrind ./appprints "Invalid read of size 4 ... Address is 0 bytes inside a block of size 40 free'd"
Code examples
Dangling iterator
#include <vector>
int main() {
std::vector<int> v{1, 2, 3};
int& first = v[0];
v.push_back(4); // may reallocate -> 'first' now points to freed memory
return first; // heap-use-after-free READ of size 4
// fix: copy the value (int first = v[0];) or use indices
}How to diagnose
- Access — Where is the freed memory touched?
- Free — Which line freed it?
- Owner — Who is supposed to own this object?
🔧 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
- 0xC0000005 The application was unable to start correctly (0xc0000005) / Exception code 0xc0000005 access violation
- AddressSanitizer: heap-buffer-overflow C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N
- 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