奇怪的分段违规

时间:2012-09-18 13:40:53

标签: c++ crash segmentation-fault stack-trace

我的日志发生了崩溃:

15:21:12  1645 Wrk-0.14 | *** Break ***: segmentation violation

堆栈跟踪是:

===========================================================
There was a crash.
This is the entire stack trace of all threads:
===========================================================
...
#4  <signal handler called>
#5  0x00002b5eeef98865 in HiggsSelector::RejectBadJet (this=0x1de241a0, 
    index_jet=0, index_leading=0, index_subleading=1) at HiggsSelector.C:2375

功能是:

bool HiggsSelector::RejectBadJet(int index_jet, int index_leading, int index_subleading) const
{
    assert(index_jet >= 0);
    assert(index_leading >= 0);
    assert(index_subleading >= 0);
    assert(PV_z);
    int index_PV_ID_chosen=0; //<-----in your header
    double DiPhoton_zcommon=z_common_corrected(index_leading,index_subleading,false);
    float minimal_distance=9999;
    for (unsigned int index_PV=0;index_PV<PV_z->size()-1;index_PV++) {
      if ( fabs((*PV_z)[index_PV]-DiPhoton_zcommon)<minimal_distance) {

最后一行是2375号。我真的不知道这次崩溃是怎么回事,我想我已经用assert检查了所有事情。 PV_z*std::vector<float>

2 个答案:

答案 0 :(得分:2)

如果PV_z->size() == 0,则PV_z->size()-1下溢到UINT_MAX,您可能会轻易获得分段违规,因为for循环条件始终为真。

解决问题的一种方法:

for (unsigned int index_PV=0; !PV_z->empty() && index_PV<PV_z->size()-1;index_PV++) {
                            //^^^^^^^^^^^^^^^^^^

答案 1 :(得分:1)

不要丢弃PV_z将指向月球,这将绕过断言。