SIGSEGV within std::sort, how to narrow it down

一个人想着一个人 提交于 2019-12-06 13:51:59
#0  0x000000000040b2a0 in Town::get_cur_capacity (this=0x0) at ./solver/Darstellung.cpp:98

It looks like you're invoking get_cur_capacity on a NULL pointer. You're getting here from a comparator:

#1  0x000000000040b9ab in Town::compare_by_capacity (eins=0x0, zwei=0x0) at ./solver/Darstellung.cpp:135

which is comparing NULL to NULL. You further get here from a sort:

#5  0x000000000040de63 in std::sort<__gnu_cxx::__normal_iterator<Town**, std::vector<Town*> >, bool (*)(Town const*, Town const*)> (__first=..., __last=..., __comp=0x40b98e <Town::compare_by_capacity(Town const*, Town const*)>)
    at /usr/include/c++/4.5/bits/stl_algo.h:5250

This is sorting std::vector<Town*>, and is called from:

#6  0x000000000040ce5a in Solution_Stack::get_towns_by_capacity (this=0x7fffffffe010) at ./solver/Darstellung.cpp:331

Most likely the vector you're sorting contains NULL pointers, and your compare_by_capacity function is not prepared to handle this eventuality. Either make sure the vector has no NULLs, or have compare_by_capacity explicitly check for NULL and do something sensible (eg, sort it before anything other than another NULL).

#1  0x000000000040b9e9 in Town::compare_by_index (eins=0x40, zwei=0x73b4d0) at ./solver/Darstellung.cpp:139

This 0x40 looks like you have either uninitialized memory or corrupted memory. How are you resizing this vector?

It's really quite difficult to try to diagnose this without actually seeing any of your code.

If your code is compiled with debugging symbols and without optimization, then we can probably believe the Town::get_cur_capacity (this=0x0) line which means you got a null pointer in your vector somewhere. The code that put it in may have run at a totally different point in time.

If nulls are actually intended to be allowed in your vector then your sort predicate compare_by_capacity needs to be prepared to handle that eventuality.

In this case you may need to review the code that populates your vector, and valgrind may help you track down if there are memory problems in your code.

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