Access Violation error in C++ [closed]
I have been trying to resolve the access violation in the program for bounding volume hierarchy. I get an access violation error while running it. I have used SDL and lib3d packages.
Error: Unhandled exception at 0x00fa2e80 in bvh.exe: 0xC0000005: Access violation reading location 0x00000004.
Call Stack :> 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)
Line 393 + 0x150 bytes C++
I have defined my structures in the following manner:
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;}
}
};
You don't "avoid access violation error" by changing settings in your IDE.
You resolve these faults by fixing your code. You are corrupting memory somewhere, dereferencing an invalid pointer, writing/reading past the end of a buffer, free
ing memory that was already freed....
I seriously doubt that 0x00000004
is pointing to something. Usually numbers like that don't; they are too "normal". You can't dereference an int
.
Looks like you're trying to access the second member variable of the Triangle in that member function, and the this
pointer is NULL.
Or maybe once of those vertex pointers passed in is NULL.
As an example, this would produce the error you are seeing:
struct Foo
{
void bar() { cout << b << end; }
int a, b;
};
int main()
{
Foo* f = 0;
f->bar();
return 0;
}
The access to b
in bar()
would is effectively an access to an int
offset by 4
(sizeof(int)
) to this
. If this
is NULL
(as it is in the example) then you would get an access violation at location 0x00000004.
Ensure that all your pointers are valid.
精彩评论