double free or corruption 🔧 C/C++

C/C++: free(): double free detected / double free or corruption (out) — Aborted (core dumped)

Memory was freed twice or the heap was corrupted (buffer overflow, freeing a non-heap pointer), so glibc aborted the program.

Seen on: C/C++

Meaning

Common sources: two owners freeing the same pointer, copying a class with a raw pointer member (shallow copy then two destructors), writing past an array’s end, or delete on stack memory. AddressSanitizer pinpoints it.

Common causes

  • Same pointer freed/deleted twice
  • Shallow copy of a class owning raw memory (rule of three)
  • Buffer overflow corrupting heap metadata
  • Freeing memory not from malloc/new

⚡ Quick fix

  1. Build with -fsanitize=address -g and run again
  2. Use std::unique_ptr/std::vector instead of raw new/delete
  3. Set pointers to nullptr after free

Detailed fix by platform

Shell

  1. cpp
    g++ -g -fsanitize=address -fno-omit-frame-pointer main.cpp -o app && ./app
    valgrind --leak-check=full ./app

How to diagnose

  1. ASan — Report of first invalid free/write
  2. Ownership — Who frees the memory?

🔧 Still not fixed?

Many errors look alike. If the steps above didn’t solve it, one of these is probably what you’re facing:

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