2013-01-02 85 views
1

當我嘗試在cygwin中運行我的程序時,我所做的以下功能導致錯誤「Aborted(核心轉儲)」。我嘗試使用gdb的,我得到:「節目的接收信號SIGABRT,中止 00000000的?()」中止(核心轉儲)C++ Cygwin

我已經試過先擦除你我,但這個產生相同的結果,在第二次擦除期間崩潰(在這種情況下擦除(我))。

CoalesceOverlaps函數的基本思想是將自身重疊的OVERLAP組合在一起,所以我對列表進行排序,檢查兩個相鄰的元素(如果它們重疊),並且如果它們重疊,則創建一個組合的新元素我的&你,刪除我&你並用新的組合元素替換它們。

該函數的邏輯工作原理是當我用一個手工完成的元素的硬編碼列表測試它時,輸出結果是正確合併的,但是當我嘗試在具有大列表/空列表等的真實程序中實現它時,它失敗。

EDIT(來自cnvr_check_v1.1.exe.stackdump):

Stack trace: 
Frame  Function Args 
0028A624 76E31194 (000000E8, 0000EA60, 00000000, 0028A758) 
0028A638 76E31148 (000000E8, 0000EA60, 000000A4, 0028A734) 
0028A758 610DC559 (00000001, 80038390, 0000001D, 610EBCCC) 
0028A848 610D9913 (00000000, 0028A890, 0028A878, 61187784) 
0028A8A8 610D9DEE (0028FF14, 00000001, 0028A8E8, 00000006) 
0028A958 610D9F40 (00000158, 00000006, 0053002B, 61187784) 
0028A978 610D9F6C (00000006, 00000006, 0028A9A8, 610B66D1) 
0028A9A8 610DA233 (00000000, 0028A9DC, 0028A9C8, 610FD3CA) 
End of stack trace 

的代碼:

void CoalesceOverlaps(list<OVERLAP>& overlap_regions) 
{ 
cout << "Begining of CoalesceOverlaps function\n"; 
overlap_regions.sort(OVERLAPStartSortPredicate); 
//now coalesce 
cout << "Didn't fail during sorting\n"; 

list<OVERLAP>::iterator me = overlap_regions.begin(), 
          end = overlap_regions.end(); 

if (me != end) // Treat empty list 
    for(list<OVERLAP>::iterator thee = ++me; // Post-increment 
     thee != end; 
     me++, thee++) 
    { 

     cout << "just before thee-> start less than... if\n"; 
     //cout << me->stop << endl; 
     if(thee->start <= me->stop) //hit to coalesce them 
     { 
      cout << "thee->ID:" << thee->id << endl; 
      cout << "thee->start:" << thee->start << endl; 
      cout << "made it to the thee->start less than me->stop if\n"; 
      long temp_start = min(thee->start,me->start),temp_stop = max(thee->stop,me->stop); 
      OVERLAP temp_region; 
      temp_region.start = temp_start; 
      temp_region.stop = temp_stop; 
      cout << "just before the first erase\n"; 
      //overlap_regions.push_front(temp_region); 
      list<OVERLAP>::iterator temp_itr = overlap_regions.erase(me); 

      cout << "thee->ID:" << thee->id << endl; 
      cout << "thee->start:" << thee->start << endl; 

      cout << "just before the second erase\n"; 
      //cout << thee->id; 
      overlap_regions.erase(thee); 
      cout << "past the erases\n"; 
      overlap_regions.insert(temp_itr,temp_region); 
     } 
     cout << "bottom of the for\n"; 
    } 
cout << "End of CoalesceOverlaps function\n"; 

} 

EDIT(下面校正功能)謝謝!:

void CoalesceOverlaps(list<OVERLAP>& overlap_regions) 
{ 
overlap_regions.sort(OVERLAPStartSortPredicate); 

//iterators for keeping track of the two nodes we are comparing 
list<OVERLAP>::iterator me = overlap_regions.begin(), 
         thee = overlap_regions.begin(), 
          end = overlap_regions.end(); 


if (me != end) // Treat empty list 
    thee++; //sets it to the second element 
    if(thee!=end) //Treat list with one element 
     while(thee != end) //lets keep comparing until we right the end 
     { 
      if(thee->start <= me->stop) //hit to coalesce them 
      { 
       long temp_start = min(thee->start,me->start),temp_stop = max(thee->stop,me->stop); 
       OVERLAP temp_region; 
       temp_region.start = temp_start; 
       temp_region.stop = temp_stop; 

       overlap_regions.erase(me); 

       list<OVERLAP>::iterator temp_itr = overlap_regions.erase(thee); 

       me = overlap_regions.insert(temp_itr,temp_region); 
       thee = temp_itr; 
      } 
      else{ 
       me++; 
       thee++; 
      } 
     } 
} 
+0

你還可以提供轉儲嗎? –

+0

您是否嘗試在調試器下運行它以查看錯誤所在(gdb等)? – slugonamission

+0

@izomorphius - 我編輯原始帖子以包含轉儲。 – Robert

回答

2

我相信你的me擦除會使你的thee迭代器失效。

for(list<OVERLAP>::iterator thee = ++me; // Post-increment 

這保證它們是你的初始化器中的同一個節點。你的評論說,後增量。即不是後增量。因此,你再這樣做:

list<OVERLAP>::iterator temp_itr = overlap_regions.erase(me); 

本緊跟:

cout << "thee->ID:" << thee->id << endl; 
cout << "thee->start:" << thee->start << endl; 

...等。擦除'我'後,thee不再有效。訪問它來讀取是未定義的行爲,但可能會工作,因爲仍有在引用的數據指針。但它是無限的未定義的行爲,最終通過erase()調用表現出來。

+0

但我刪除後,cout << "thee-> ID:「​​id << endl; cout << "thee-> start:」​​start << endl;仍然有效 – Robert

+1

看起來就是這樣,因爲節點還沒有被覆蓋。另一個問題是for語句中的「me ++,the ++」。擦除後無法做到這一點。 –

+0

@羅伯特見答案的最後一段。一旦你從迭代器中刪除了元素,我相信你處於未定義行爲(這意味着你不能依賴它工作,甚至不能工作)。我相信它最終會在清除電話中表現出來(可怕)。 – WhozCraig