iterator invalidation C/C++ Origin: Application & API OSI layer 7 · Application Last updated 2 Dec 2026

Undefined behaviour: using an invalidated iterator after push_back / erase on std::vector

An iterator, pointer or reference into a container was used after an operation that invalidated it.

Exact message you may see iterator invalidationattempt to dereference a singular iteratorvector iterators incompatibleiterator invalidated
Seen on: C/C++

Meaning

push_back can reallocate a vector, invalidating every iterator and pointer. erase invalidates iterators at and after the erased position. The result is crashes or silent garbage, often only in release builds.

Common causes

  • push_back inside a range-for over the same vector
  • Keeping a pointer to an element across insertions
  • Erasing in a loop with a stale iterator
  • Using end() saved before modification

⚡ Quick fix

  1. Use it = v.erase(it) pattern
  2. Reserve capacity up front or use indices
  3. Iterate over a copy when modifying
  4. Build with -D_GLIBCXX_DEBUG

Detailed fix by platform

GCC

  1. g++ -g -D_GLIBCXX_DEBUG -fsanitize=address main.cpp aborts with an "attempt to dereference a singular iterator" message

Code examples

erase while iterating

cpp
for (auto it = v.begin(); it != v.end(); ++it)
    if (*it == 0) v.erase(it);          // invalidates it
for (auto it = v.begin(); it != v.end();)
    it = (*it == 0) ? v.erase(it) : it + 1;   // correct

How to diagnose

  1. Mutation — Container modified in the loop?
  2. Saved — Iterators/pointers kept?
  3. Debug — _GLIBCXX_DEBUG result?

🔧 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):

AddressSanitizer: heap-use-after-free — C/C++: ==ERROR: AddressSanitizer: heap-use-after-free on address — memory used after delete/free (C/C++)AddressSanitizer: hea…call of overloaded is ambiguous — C++: error: call of overloaded 'f(int)' is ambiguous (C/C++)call of overloaded is…cannot declare variable to be of abstract type — C++: error: cannot declare variable 'x' to be of abstract type 'Shape' (pure virtual functions) (C/C++)cannot declare variab…dangling — warning: object backing the pointer will be destroyed at the end of the full-expression (std::string_view from a temporary) (C/C++)danglingDebug Assertion Failed — Debug Assertion Failed! Expression: vector subscript out of range (C/C++)Debug Assertion FailedE0271: type mismatch resolving associated type — Rust: error[E0271]: type mismatch resolving `<impl Iterator as Iterator>::Item == i32` (Rust)E0271: type mismatch …reference to is ambiguous — C++: error: reference to 'X' is ambiguous (using namespace std conflicts) (C/C++)reference to is ambig…region FLASH overflowed — arm-none-eabi-ld: region `FLASH' overflowed by N bytes (C/C++)region FLASH overflow…iterator …

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.