C ++异常访问冲突

时间:2012-08-22 17:42:18

标签: c++ exception exception-handling access-violation

此代码运行正常但在非特定时刻我在行

获取异常访问冲突
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()返回的Type的定义是

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

2 个答案:

答案 0 :(得分:4)

看起来像if语句中的拼写错误

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

其中一个lhs应该是rhs

答案 1 :(得分:3)

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

您有复制粘贴错误。那应该是:

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

和其他比较类似。

因此,您引用了一个空指针,给出了未定义的行为;在这种情况下,访问冲突。这不是C ++异常,因此无法使用catch块进行处理。我相信微软的C ++方言提供了一些非标准的方法来捕捉和恢复它们(__try__catch,可能),但修复bug比使用那种废话要好得多。