Search code examples
c++memory-managementstdvectorglibc

"glibc free(): invalid next size(fast)" on vector.push_back?


When I run my program it will occasionally crash and give me this error: "glibc detected /pathtoexecutable: free(): invalid next size (fast)"

The backtrace leads to a member function that just calls a vector's push_back function -

void Path::add(Position p) {path.push_back(p);}

I have tried googling the error and the very large majority of the problems are people allocating too little memory. But how could that be happening on an std::vector<>.push_back? What can I check for? Any help is appreciated.


Solution

  • You're probably doing an invalid write somewhere and trashing the control information kept by glibc for bookkeeping. Thus, when it tries to free things it detects abnormal conditions (unreasonable sizes to free).

    What's worst about this kind of thing is that the problem doesn't manifest itself at the point where you made the real mistake so it can be quite hard to catch (it's quite common to be an off-by-one error).

    Your best bet is to use a memory debugger. valgrind could be a start (since you mentioned glibc). Look for "invalid write of size..." before the glibc message.