0

I am new to C++, and I am trying to program a replica of Pong using freeglut with Visual Studio 2010. My code was working fine until I made some revisions to it in order to make it more object oriented. When I ran it again, it worked a couple times, until I made some minor edit (to be honest, I can't remember what it was that I changed, since I only built my code the next morning), and I received this runtime error as soon as I ran the program:

Unhandled exception at 0x1000bbae in Pong.exe: 0xC0000005: Access violation writing location 0x000000a8.

I'm not particularly good at debugging, but I believe this is coming at the line glutDisplayFunc() in my main loop. I think this has something to do with null pointers, but I have no idea what. Could somebody help me find my problem?

Below is the applicable portion of my program (Main.cpp):

include "header.h"

using namespace std;

int main(int argc, char** argv) {
    //Initialize GLUT
    glutInit(&argc, argv);
    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB | GLUT_DEPTH);
    glutInitWindowSize(WIDTH, HEIGHT); //Set the window size

    //Create the window
    //modesetup(2);
    //Set handler functions for drawing, keypresses, and window resizes
    glutDisplayFunc(mainDraw);
    glutKeyboardFunc(handleKeypress);
    glutKeyboardUpFunc(handleKeyup);
    glutSpecialFunc(handleSpecial);
    glutSpecialUpFunc(handleSpecialUp);
    glutReshapeFunc(handleResize);
    glutTimerFunc(25, update, 0);

    glutMainLoop(); //Start the main loop.  glutMainLoop doesn't return.
    return 0; //This line is never reached
}

Thanks in advance!

EDIT: I just made a new program that uses very standard functions for everything, but I still get this error. I'm beginning to think that there may be something wrong with my freeglut installation.

Draksis
  • 167
  • 2
  • 9
  • 1
    What happens when you set a breakpoint at the `glutDisplayFunc` line and just walk through the program from that point? And we'd probably need to see some/all of `mainDraw` to get a better feel for what the problem might be. – j.w.r Jun 15 '11 at 02:29
  • 2
    You desperately need to use the debugger to get a specific line or even function. We can't help you with anything when you show us code like this- it doesn't even have any real logic in it. – Puppy Jun 15 '11 at 02:32
  • I thought you would say that. When I set a breakpoint at glutDisplayFunc and press F11 (I think that's what I am supposed to do), the error immediately pops up. For testing purposes, I have completely commented out the code in mainDraw. – Draksis Jun 15 '11 at 02:52

3 Answers3

1

Run your program under valgrind. It will give you a lot more information about the problem, and might even point out memory errors other than the immediate cause of the crash.

John Zwinck
  • 239,568
  • 38
  • 324
  • 436
1

Use F5 to run the program with debugging in visual studio. When you get the error the debugger should place you right on the line with the illegal access. If it doesn't have source code for the location then check up the call stack.

Cheers & hth.

Cheers and hth. - Alf
  • 142,714
  • 15
  • 209
  • 331
1

You probably already solved the problem. But since I had the same problem and couldn t find an answer two it anywhere I will answer the question:

You simply forgot to write the

glutCreateWindow("Windowname");

function before the

glutDisplayFunc(mainDraw);
glutKeyboardFunc(handleKeypress);
glutKeyboardUpFunc(handleKeyup);
glutSpecialFunc(handleSpecial);
glutSpecialUpFunc(handleSpecialUp);
glutReshapeFunc(handleResize);
glutTimerFunc(25, update, 0);

part. This leads to the exception you get.

Mark Hall
  • 53,938
  • 9
  • 94
  • 111