AddressSanitizer: heap-use-after-free C/C++ Origin: Application & API Last updated 15 Oct 2026

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.

Exact message you may see AddressSanitizer: heap-use-after-freeheap-use-after-free on addressfreed by thread T0 herepreviously allocated by thread T0 hereAddressSanitizer: stack-use-after-scope
Seen on: C/C++ GCC Clang

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

  1. Compare the access stack with the "freed by" stack
  2. Replace raw owning pointers with unique_ptr/shared_ptr
  3. Don't keep references or iterators across insertions

Detailed fix by platform

GCC / Clang

  1. Build with -fsanitize=address -g and run the failing case; set ASAN_OPTIONS=detect_stack_use_after_return=1 for stack variants

Linux

  1. valgrind ./app prints "Invalid read of size 4 ... Address is 0 bytes inside a block of size 40 free'd"

Code examples

Dangling iterator

cpp
#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

  1. Access — Where is the freed memory touched?
  2. Free — Which line freed it?
  3. 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:

Where this error sits

Origin: Application & API. Errors that are often confused with or caused by this one (click to open):

0xC0000005 — The application was unable to start correctly (0xc0000005) / Exception code 0xc0000005 access violation (Windows)0xC0000005AddressSanitizer: heap-buffer-overflow — C/C++: ==ERROR: AddressSanitizer: heap-buffer-overflow on address — READ/WRITE of size N (C/C++)AddressSanitizer: hea…Bus error — C/C++: Bus error (core dumped) — SIGBUS from misaligned access or a bad memory-mapped file (C/C++)Bus errorC2065 — MSVC: error C2065: 'x': undeclared identifier (C/C++)C2065Cannot allocate memory — fork: Cannot allocate memory / ENOMEM (Linux)Cannot allocate memorydouble free or corruption — C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped) (C/C++)double free or corrup…GLIBCXX not found — Linux: version `GLIBCXX_3.4.30' not found (required by ./app) — libstdc++.so.6 (C/C++)GLIBCXX not foundis private within this context — C++: error: 'int X::secret' is private within this context (C/C++)is private within thi…AddressSa…

Explore the map of this error Full error map →

🧠 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.