C ++中的访问冲突错误

时间:2011-06-12 19:09:13

标签: c++ visual-studio-2010

我一直在尝试解决程序中的访问冲突,以限制卷层次结构。运行时出现访问冲突错误。我使用过SDL和lib3d软件包。

  

错误:bvh.exe中0x00fa2e80处的未处理异常:0xC0000005:访问冲突读取位置0x00000004。

调用堆栈:> bvh.exe!Triangle::Triangle(const Vertex * vertexA, const Vertex * vertexB, const Vertex * vertexC, unsigned int r, unsigned int g, unsigned int b, bool twosided, bool triNormalProvided, Float3 triNormal)第393行+ 0x150字节C ++

我已按以下方式定义了我的结构:

struct Float3 
{
   float X, Y Z;
   Float3(float x=0, float y=0, float z=0)
        :
    X(x), Y(y), Z(z){} 

   Float3(const Float3& rhs)
        :
    X(rhs.X), Y(rhs.Y), Z(rhs.Z){}

    inline Float3& operator+=(const Float3& rhs)
    {
    X += rhs.X; Y += rhs.Y; Z += rhs.Z; return *this;
    }

    // similar for the other operators

    void assignSmaller(const Float3& rhs)
   {
        X = min(X, rhs.X);
        Y = min(Y, rhs.Y); 
        Z = min(Z, rhs.Z);
    }

   void assignBigger(const Float3& rhs)
        {
        X = max(X, rhs.X);
        Y = max(Y, rhs.Y);
        Z = max(Z, rhs.Z);
        }

    float length()
    {
    return sqrt(X*X +Y*Y + Z*Z);
    }

    inline float lengthsq()
    {
    return X*X +Y*Y + Z*Z;
    }

    inline void Normalize()
    {
    float norm = length();
    X/= norm; Y/= norm; Z/= norm;
    }
};

struct Vertex : public Float3
{
    Float3 n;
    unsigned _ambientOcclusionCoeff;

    Vertex(float x, float y, float z, float nx, float ny, float nz, unsigned char amb=60)
    :
    Float3(x,y,z), n(nx,ny,nz), _ambientOcclusionCoeff(amb){}

};

struct Pixel {
    float _b, _g, _r;
    Pixel(float r=0.f, float g=0.f, float b=0.f)
    :
    _b(b), _g(g), _r(r) {}

Pixel& operator+=(const Pixel& rhs) { _b += rhs._b; _g += rhs._g; _r += rhs._r; return *this; }
     // similar for the other operators

};

struct Triangle
{
    const Vertex *_vertexA, *_vertexB, *_vertexC;
    Float3 centroid, n; 
    // Color: 
    Pixel _colorf; 
    // precomputed for SDL surface
    Uint32 _color;

    // Should we backface cull this triangle?
    bool _twoSided;

    // Raytracing intersection pre-computed cache:
    float _d, _d1, _d2, _d3;
    Float3 _e1, _e2, _e3, _bottom, _top;

    Triangle(
    const Vertex *vertexA,
        const Vertex *vertexB,
    const Vertex *vertexC,
    unsigned r, unsigned g, unsigned b,
    bool twosided = false, bool triNormalProvided=false,
    Float3 triNormal=Float3(0.0f,0.0f,0.0f)) 
     :

    _vertexA(vertexA), _vertexB(vertexB), _vertexC(vertexC),

    centroid((vertexA->X + vertexB->X + vertexC->X)/3.0f,
         (vertexA->Y + vertexB->Y + vertexC->Y)/3.0f,
         (vertexA->Z + vertexB->Z + vertexC->Z)/3.0f),

    _colorf((float)r,(float)g,(float)b), // For use in all other cases
    _color(SDL_MapRGB(Screen::_surface->format, r,g,b)), // For use with DrawPixel

    _twoSided(twosided),

     _bottom(FLT_MAX,FLT_MAX, FLT_MAX), // Will be updated after centering in Loader
     _top(-FLT_MAX, -FLT_MAX,-FLT_MAX) // Will be updated after centering in Loader
    {
         // this is where the debugger points to with an access violation error.
        if (!triNormalProvided) 
         {
        n = Float3((vertexA->n.X + vertexB->n.X + vertexC->n.X)/3.0f,
               (vertexA->n.Y + vertexB->n.Y + vertexC->n.Y)/3.0f,
               (vertexA->n.Z + vertexB->n.Z + vertexC->n.Z)/3.0f);
        n.Normalize();
         }
       else {n = triNormal;}
        }
};

3 个答案:

答案 0 :(得分:8)

您不会通过更改IDE中的设置来“避免访问冲突错误”。

您可以通过修复代码来解决这些问题。你正在某处破坏内存,取消引用无效指针,写入/读取缓冲区的末尾,free已经释放的内存....

答案 1 :(得分:4)

我严重怀疑0x00000004指的是什么。通常这样的数字不会;他们太“正常”了。您无法取消引用int

答案 2 :(得分:3)

看起来你正试图在该成员函数中访问Triangle的第二个成员变量,this指针为NULL。

或者传入的顶点指针中的一次是NULL。

例如,这会产生您看到的错误:

struct Foo
{
    void bar() { cout << b << end; }
    int a, b;
};

int main()
{
    Foo* f = 0;
    f->bar();
    return 0;
}

bbar()的访问权限实际上是int4)到sizeof(int)的{​​{1}}偏移量的访问权限。如果thisthis(如示例中所示),那么您将在位置0x00000004处获得访问冲突。

确保所有指针都有效。