Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 482
Sterling Avatar asked Sep 03 '25 01:09

Sterling


2 Answers

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.

like image 177
cnicutar Avatar answered Sep 05 '25 06:09

cnicutar


As @cniculat sayed, try valgrind.

Another tools you can try are:

  • gcc stl debug support. If ther problem in incorrect usage of STL container, tahn compiling with D_GLIBCXX_DEBUG and -D_GLIBCXX_DEBUG_PEDANTIC may reveal the problem. In case the problem will be discovered, program will be aborted by assert(), so you'll receive error message on console.
  • Yet another option is to use google tcmalloc. It overrides malloc()/free(). Just link your appl with tcmalloc link version, and it can detect mist memory usage problems.

STL debug support and tcmalloc can be used in regular in debug builds. This way you can work as regular, while these tools will in "background" assert you if there's an error.

like image 34
dimba Avatar answered Sep 05 '25 07:09

dimba