2012-08-22 175 views
1

該代碼可以正常運行,但在一個非特定時刻IM在行C++異常訪問衝突

return lhs.getGeneralType()->getID() != rhs.getGeneralType()->getID(); 

和錯誤打破我的申請得到一個異常訪問衝突 ...香港專業教育學院已經嘗試過用編譯/ EHA但錯誤

重複......這行代碼正確運行多次,一段時間後,會出現此錯誤..

if (lhs.getType() == rhs.getType()) { 

    try { 

     if (lhs.getGeneralType() != NULL && lhs.getGeneralType() != NULL) 
      return lhs.getGeneralType()->getID() != rhs.getGeneralType()->getID(); 
     else if (lhs.getGeneralType() == NULL && lhs.getGeneralType() == NULL) 
      return false; 
     else if (lhs.getGeneralType() != NULL && lhs.getGeneralType() == NULL) 
      return false; 
     else if (lhs.getGeneralType() == NULL && lhs.getGeneralType() != NULL) 
      return true; 
     else 
      return true; 

    } catch(char * e) { // tried also exception & , char* e 
     return true; 
    } 
} else 
    return true; 

,其類型getGeneralType()返回的定義是低於

class Type 
    { 
    private: 
    int _id; 
    public: 
    Type(int id); 
    operator int() const; 
    int getID() const; 
    }; 

回答

4

它看起來像if語句拼寫錯誤

if (lhs.getGeneralType() != NULL && lhs.getGeneralType() != NULL) 

其中lhs的的也許應該是rhs

3
if (lhs.getGeneralType() != NULL && lhs.getGeneralType() != NULL) 

您有複製粘貼錯誤。這應該是:

if (lhs.getGeneralType() != NULL && rhs.getGeneralType() != NULL) 
            ^

和其他比較類似。

因此,您引用了一個空指針,給出了未定義的行爲;在這種情況下,訪問衝突。這不是C++異常,因此無法使用catch塊進行處理。我相信微軟的C++方言提供了一些非標準的方法來捕獲並從這些方面恢復(可能是__try__catch),但修復bug比使用那種無稽之談要好得多。