Unhandled exception : Access Violation

Hi Guys,

I am getting a unhandled exception : access violation.

Have been remarking things out…
Still havent fixed it, but this is where I am at:

My program runs for a little while, draws the graphics on screen, but after a few seconds throws the above error.

int DrawGLScene(GLvoid) // Here's Where We Do All The Drawing
{
	int i;

	GLfloat radius_1=1.8f;
	GLfloat radius_2=2.0f;
	GLfloat radius_3=2.2f;

//////////XSENS STUFF //////////////

	GetData();

///////////////////////////////////

	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // Clear Screen And Depth Buffer
	glLoadIdentity(); // Reset The Current Modelview Matrix
	glTranslatef(0.0f,0.0f,-7.0f); 

	//**** Disable texturing
	glDisable(GL_TEXTURE_2D);

//	glPushMatrix();  //1 push
	glRotatef(fOrientationData[0],0.0f,0.0f,1.0f);
	glTranslatef(0.0f,(fOrientationData[1]/18.0)+0.0f,0.0f);

	//**** Blue/Brown of Horizon
	glBegin(GL_QUADS); 
		glColor3f(0.0f, 0.6f, 0.796f);
		glVertex3f(-6.0f, 18.0f, 0.0f); // Top Left
		glVertex3f( 6.0f, 18.0f, 0.0f); // Top Right
		glVertex3f( 6.0, 0.0f, 0.0f); // Bottom Right
		glVertex3f(-6.0f, 0.0f, 0.0f); // Bottom Left

		glColor3f(0.694f, 0.4f, 0.0f);
		glVertex3f(-6.0f, -18.0f, 0.0f); // Top Left
		glVertex3f( 6.0f, -18.0f, 0.0f); // Top Right
		glVertex3f( 6.0f, 0.0f, 0.0f); // Bottom Right
		glVertex3f(-6.0f, 0.0f, 0.0f); // Bottom Left

	glEnd();

	//**** Horizon Lines
	
	for (lines=-18; lines<19; lines++)
	{
	glBegin(GL_LINES);
		glColor3f(1.0f, 1.0f, 1.0f); // Sets Color White

		glVertex3f(-6.0f, 0.0f, 0.0f); //Level
		glVertex3f(6.0f, 0.0f, 0.0f);

		glVertex3f(-0.75f, lines+1.0f, 0.0f); //10 UP
		glVertex3f(0.75f, lines+1.0f, 0.0f);

		glVertex3f(-0.25f, lines+0.75f, 0.0f); // ---
		glVertex3f(0.25f, lines+0.75f, 0.0f);

		glVertex3f(-0.5f, lines+0.5f, 0.0f); //---===---
		glVertex3f(0.5f, lines+0.5f, 0.0f);

		glVertex3f(-0.25f, lines+0.25f, 0.0f); // ---
		glVertex3f(0.25f, lines+0.25f, 0.0f);

	glEnd();

	}
/*
	//***** Font Stuff ******
	glPushMatrix(); // 2 push
	glColor3f(1.0f,1.0f,1.0f);
	glTranslatef(-1.1f,-0.1f,0.0f);

	for (i=10;i<90;i=i+10)
		{
		stringstream ss;
		ss << i;
		glTranslatef(0.0f,1.0f,0.0f);
		glPrint(ss.str().c_str());
		}
	glPopMatrix(); 


	glTranslatef(0.8f,-0.1f,0.0f);
	for (i=10;i<190;i=i+10)
		{
		stringstream ss;
		ss << i;
		glTranslatef(0.0f,1.0f,0.0f);
		glPrint(ss.str().c_str());
		}

	glPopMatrix();
*/
/*
	// WINGS
	glBegin(GL_QUADS); 
	glColor3f(0.0f,0.0f,0.0f);
	glVertex3f(-1.75f,0.075f,0.0f); //LEFT
	glVertex3f(-0.75f,0.075f,0.0f); 
	glVertex3f(-0.75f,-0.075f,0.0f); 
	glVertex3f(-1.75f,-0.075f,0.0f); 

	glVertex3f(-0.75f,-0.075f,0.0f);
	glVertex3f(-0.75f,-0.175f,0.0f);
	glVertex3f(-0.875f,-0.175f,0.0f);
	glVertex3f(-0.875f,-0.075f,0.0f);

	glVertex3f(1.75f,0.075f,0.0f); //RIGHT
	glVertex3f(0.75f,0.075f,0.0f); 
	glVertex3f(0.75f,-0.075f,0.0f); 
	glVertex3f(1.75f,-0.075f,0.0f); 

	glVertex3f(0.75f,-0.075f,0.0f);
	glVertex3f(0.75f,-0.175f,0.0f);
	glVertex3f(0.875f,-0.175f,0.0f);
	glVertex3f(0.875f,-0.075f,0.0f);

	glVertex3f(-0.075f,0.075f,0.0f); // MIDDLE DOT
	glVertex3f(0.075f,0.075f,0.0f);
	glVertex3f(0.075f,-0.075f,0.0f);
	glVertex3f(-0.075f,-0.075f,0.0f);
	glColor3f(1.0f, 1.0f, 1.0f); 
	glEnd();

	// OUTLINES IN WHITE

	glBegin(GL_LINE_LOOP); 
	glColor3f(1.0f,1.0f,1.0f);
	glVertex3f(-1.75f,0.075f,0.0f); //LEFT
	glVertex3f(-0.75f,0.075f,0.0f); 
	glVertex3f(-0.75f,-0.175f,0.0f);
	glVertex3f(-0.875f,-0.175f,0.0f);
	glVertex3f(-0.875f,-0.075f,0.0f);
	glVertex3f(-1.75f,-0.075f,0.0f); 
	glEnd();

	glBegin(GL_LINE_LOOP); 
	glColor3f(1.0f,1.0f,1.0f);
	glVertex3f(1.75f,0.075f,0.0f); //LEFT
	glVertex3f(0.75f,0.075f,0.0f); 
	glVertex3f(0.75f,-0.175f,0.0f);
	glVertex3f(0.875f,-0.175f,0.0f);
	glVertex3f(0.875f,-0.075f,0.0f);
	glVertex3f(1.75f,-0.075f,0.0f); 
	glEnd();

	glBegin(GL_LINE_LOOP); 
	glVertex3f(-0.075f,0.075f,0.0f); // MIDDLE DOT
	glVertex3f(0.075f,0.075f,0.0f);
	glVertex3f(0.075f,-0.075f,0.0f);
	glVertex3f(-0.075f,-0.075f,0.0f); 
	glColor3f(1.0f, 1.0f, 1.0f); 
	glEnd();

//	glPopMatrix(); // 1 pop

	// MOVING NON FILLED BANK TRIANGLE
//	glPushMatrix();      // 2 push         
	glRotatef(fOrientationData[0],0.0f,0.0f,1.0f);
	glBegin(GL_LINE_LOOP);
	glVertex3f(-0.15f,1.6f,0.0f);
	glVertex3f(-0.15f,1.5f,0.0f);
	glVertex3f(0.15f,1.5f,0.0f);
	glVertex3f(0.15f,1.6f,0.0f);
	glVertex3f(0.0f,1.8,0.0f);
	glColor3f(1.0f, 1.0f, 1.0f);
	glEnd();
//	glPopMatrix(); // 2 pop

	glBegin(GL_TRIANGLES);  // NON MOVING BANK TRIANGLE - R=1.8
	glVertex3f(0.0f,1.8,0.0f);
	glVertex3f(-0.15f,2.0f,0.0f);
	glVertex3f(0.15f,2.0f,0.0f);
	glEnd();


	glRotatef(90,0.0f,0.0f,1.0f);
	glBegin(GL_LINES);
	glVertex3f(radius_1*cos(pi/18),radius_1*sin(pi/18),0.0f); //10 deg
	glVertex3f(radius_2*cos(pi/18),radius_2*sin(pi/18),0.0f);
	glVertex3f(radius_1*cos(pi/9),radius_1*sin(pi/9),0.0f); // 20 deg
	glVertex3f(radius_2*cos(pi/9),radius_2*sin(pi/9),0.0f);
	glVertex3f(radius_1*cos(pi/6),radius_1*sin(pi/6),0.0f); // 30 deg
	glVertex3f(radius_3*cos(pi/6),radius_3*sin(pi/6),0.0f); 
	glVertex3f(radius_1*cos(pi/4),radius_1*sin(pi/4),0.0f); // 45 deg
	glVertex3f(radius_2*cos(pi/4),radius_2*sin(pi/4),0.0f);
	glVertex3f(radius_1*cos(pi/3),radius_1*sin(pi/3),0.0f); // 60 deg
	glVertex3f(radius_3*cos(pi/3),radius_3*sin(pi/3),0.0f);
	glEnd();



	pi=pi*-1;
	glBegin(GL_LINES);
	glVertex3f(radius_1*cos(pi/18),radius_1*sin(pi/18),0.0f); //10 deg
	glVertex3f(radius_2*cos(pi/18),radius_2*sin(pi/18),0.0f);
	glVertex3f(radius_1*cos(pi/9),radius_1*sin(pi/9),0.0f); // 20 deg
	glVertex3f(radius_2*cos(pi/9),radius_2*sin(pi/9),0.0f);
	glVertex3f(radius_1*cos(pi/6),radius_1*sin(pi/6),0.0f); // 30 deg
	glVertex3f(radius_3*cos(pi/6),radius_3*sin(pi/6),0.0f); 
	glVertex3f(radius_1*cos(pi/4),radius_1*sin(pi/4),0.0f); // 45 deg
	glVertex3f(radius_2*cos(pi/4),radius_2*sin(pi/4),0.0f);
	glVertex3f(radius_1*cos(pi/3),radius_1*sin(pi/3),0.0f); // 60 deg
	glVertex3f(radius_3*cos(pi/3),radius_3*sin(pi/3),0.0f);
	glEnd();

	//**** Enable texturing
	glEnable(GL_TEXTURE_2D);

	glEnable(GL_BLEND);
	glDisable(GL_DEPTH_TEST);
	glBlendFunc(GL_DST_COLOR,GL_ZERO);

	glBindTexture(GL_TEXTURE_2D, texture[1]);
	glBegin(GL_QUADS); // Start Drawing A Textured Quad
	glTexCoord2f(0.0f, 1.0f); glVertex3f(-5.0f, 5.0f, 0.0f); // Bottom Left
	glTexCoord2f(1.0f, 1.0f); glVertex3f(5.0f, 5.0f, 0.0f); // Bottom Right
	glTexCoord2f(1.0f, 0.0f); glVertex3f(5.0f, -5.0f, 0.0f); // Top Right
	glTexCoord2f(0.0f, 0.0f); glVertex3f(-5.0f, -5.0f, 0.0f); // Top Left
	glEnd(); 

	glDisable(GL_BLEND);
*/

As you can see above I have remarked out a heap of stuff and it seems to be running ok without crashing.

I really think its some sort of stack error. I cant even see where the code is breaking because it says no source code available for the current location.

As soon as I unremark any of the remarked code, the error happens again.

Anyone please help.

Thanks

-Al
PS: Full code at http://www.hstuart.dk/paste/view.aspx?id=feddb667-fc12-4b6a-917d-68698f8ffa30

I cant even see where the code is breaking because it says no source code available for the current location.
Means you don’t have a valid call stack at the time of the crash?
Look at the instruction pointer and compare with the module load addresses to find the module.
If you jumped to an invalid address, that won’t help.

I would always be careful with strings.
For example, your program will break if the printed string is longer than 256.
Does it also crash if you use only the commented print part above?
What about not calling your data input routine GetData()?
Just comment and uncomment in binary search manner.

Other things: You should not put glColor calls at the very end of a glBegin-glEnd. Move them outside. Same for primitives where all vertices have the same color, move it before the glBegin.
And avoid redundant calls, the current color is an OpenGL state.

What’s your OpenGL Implementation? (Check all glGetString infos.)

Careful with GL_RGB textures. Watch your glPixelStore UNPACK_ALIGNMENT.

If you call CreateFont() you should also call DeleteObject() later on or you’ll leak GDI resources.

I’d frown upon “render in the idle loop” constructs. This will kill 100% of your CPU even if you don’t need to redraw stuff.
Can your GetData() stuff handle that so often?

just a thought:

 
 glColor3f(1.0f, 1.0f, 1.0f); // Sets Color White

 glBegin(GL_LINES);

  glVertex3f(-6.0f, 0.0f, 0.0f); //Level
  glVertex3f(6.0f, 0.0f, 0.0f);

  for (lines=-18; lines<19; lines++) {

	glVertex3f(-0.75f, lines+1.0f, 0.0f); //10 UP
	glVertex3f(0.75f, lines+1.0f, 0.0f);

	glVertex3f(-0.25f, lines+0.75f, 0.0f); // ---
	glVertex3f(0.25f, lines+0.75f, 0.0f);

	glVertex3f(-0.5f, lines+0.5f, 0.0f); //---===---
	glVertex3f(0.5f, lines+0.5f, 0.0f);

	glVertex3f(-0.25f, lines+0.25f, 0.0f); // ---
	glVertex3f(0.25f, lines+0.25f, 0.0f); }

 glEnd();

has better performance than

for (lines=-18; lines<19; lines++) {
	glBegin(GL_LINES);
		glColor3f(1.0f, 1.0f, 1.0f); // Sets Color White

		glVertex3f(-6.0f, 0.0f, 0.0f); //Level
		glVertex3f(6.0f, 0.0f, 0.0f);

		glVertex3f(-0.75f, lines+1.0f, 0.0f); //10 UP
		glVertex3f(0.75f, lines+1.0f, 0.0f);

		glVertex3f(-0.25f, lines+0.75f, 0.0f); // ---
		glVertex3f(0.25f, lines+0.75f, 0.0f);

		glVertex3f(-0.5f, lines+0.5f, 0.0f); //---===---
		glVertex3f(0.5f, lines+0.5f, 0.0f);

		glVertex3f(-0.25f, lines+0.25f, 0.0f); // ---
		glVertex3f(0.25f, lines+0.25f, 0.0f);

	glEnd();}

you will probably not notice any difference in performance, because you draw only a few lines, but anyway you should remember: always put constant expressions outside a loop.

Relic: It doesnt crash if I run it as is. As soon as I add any of the commented out stuff, then it crashes.

Given what I have said above, do you still think that it could be to do with GetData?

The GetData function does have a routine to first check for newdata, so I think it should handle regular calls ok?

Re looking at the instruction pointer and module load address, I dont understand (i am pretty green) - can you be more explicit pls? I am in VC++ Express.

Thanks

-Al

I got that part.

I have no idea what GetData() does and everything not involved with OpenGL in crashing OpenGL apps is suspicious. :wink:
It sounded as if this reads from some other hardware and you should make sure this is not involved in the crash.

I’m not explaining how to use a debugger, that’s what manuals are for.
In MSVC++ there is a whole Debug menu with options to view modules and their address range. While crashed in the debugger, look at the disassembly, it shows the location (same as eip register).
Use that to isolate the crashing module if that is possible.

Try to isolate the least amount of code which crashes by adding stuff in small pieces.

There is still the chance that this is an OpenGL bug. That’s why I asked which implementation you use. Clean up your OpenGL calls as I said first.

I’ve noticed you have 3x pushMatrix and 4x popMatrix. Shouldn’t crash the application, but who knows.
Just a guess - you run into stack corruption somewhere - typical result of stack corruption is executing some trash instead of code - this is why debugger cannot locate where the exception is. In that case my bet would be on GetData() function.