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

落爺英雄遲暮 提交于 2019-12-19 10:18:10

问题


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.


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/7276345/glibc-free-invalid-next-sizefast-on-vector-push-back

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!