2015-06-02 76 views
0

我收到一個奇怪的segfault錯誤。我粘貼了以下代碼的相關部分。奇怪的seg故障錯誤

當我在gdb運行代碼我得到以下輸出:

DERPE 
DERPH0 1 

Program received signal EXC_BAD_ACCESS, Could not access memory. 
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3ffff0 
0x000000010000fcee in Interval::contains (this=0x100103c90, other={isValid = 93, s = 1, e = 7, n = 5}) at interval.cpp:33 
33  if (!contains(other.s)) { 

A.cpp

void A::foo(){ 
    ... 
    std::cout << "DERPE" << std::endl; 
    std::cout << "DERPH" << id << " " << curr << std::endl; 
    \\graph is a vector<B> 
    std::cout << "DERPI" << id << " " << curr << " " << graph[id].bar(graph[curr]) << std::endl; 
    ... 
} 

B.cpp

bool B::bar(B &other) const { 
    // each B contains 2 Intervals, adj, opp 
    return (other.adj.contains(this->adj) and 
      (!this->opp.isValid or other.adj.contains(this->opp) or 
      (other.opp.isValid and other.opp.contains(this->opp)))); 
} 

Interval.cpp

bool Interval::contains(Interval other) const { 
    if (!contains(other.s)) { 
    return false; 
    } 
    ... 
} 

當我嘗試更動GDB我得到如下:

(gdb) p other 
$1 = { 
    isValid = 93, 
    s = 1, 
    e = 7, 
    n = 5 
} 
(gdb) p this 
$2 = (const Interval *) 0x100103c90 
(gdb) p *this 
$3 = { 
    isValid = 93, 
    s = 1, 
    e = 0, 
    n = 5 
} 
(gdb) p other.s 
$4 = 1 
(gdb) p (*this).contains(other.s) 

Program received signal EXC_BAD_ACCESS, Could not access memory. 
Reason: KERN_PROTECTION_FAILURE at address: 0x00007fff5f3fff50 
Interval::contains (this=Cannot access memory at address 0x7fff5f3fff50 
) at interval.cpp:28 
28 bool Interval::contains(int x) const { 
The program being debugged was signaled while in a function called from GDB. 
GDB remains in the frame where the signal was received. 
To change this behavior use "set unwindonsignal on" 
Evaluation of the expression containing the function (Interval::contains(int) const) will be abandoned. 
(gdb) p this 
Cannot access memory at address 0x7fff5f3fff50 

對如何處理這個任何想法?我迷路了。

編輯: 區間的完整標題文件,如下所示:

Interval.h

#ifndef INTERVAL_H 
#define INTERVAL_H 

#include <vector> 

    struct Interval { 
     bool isValid; 
     int s, e, n; 

     Interval() {}; 
     Interval(int n); 
     Interval(int start, int end, int n); 
     bool operator==(const Interval other) const; 
     int length() const; 
     bool contains(int x) const; 
     bool contains(Interval other) const; 
    }; 
#endif 

我沒有拷貝構造函數,但我想,一切都只是原語,所以默認的複製構造函數將工作。

我與它的一些更多的發揮各地,這似乎相關:

(gdb) backtrace 10 
#0 0x000000010000fcce in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:33 
#1 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#2 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#3 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#4 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#5 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#6 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#7 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#8 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
#9 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52  
(gdb) backtrace 
#0 0x000000010000fcce in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:33 
... 
#67667 0x000000010000fd9b in Interval::contains (this=0x100103c90, other={isValid = 34, s = 1, e = 6, n = 5}) at interval.cpp:52 
(gdb) 
+0

'A :: foo'中使用的索引是否正確?你能不能顯示'Interval'的實際定義? –

+3

'bool Interval :: contains(Interval other)'按值傳遞'Interval'需要'Interval'具有正確的複製語義。請張貼這個類的定義。 – PaulMcKenzie

回答

1

我想通了。無限回溯意味着包含的遞歸處理不當(我遺漏了一個基本情況),所以它無限遞歸 - >堆棧溢出 - > seg故障。