PDA

View Full Version : Access Violation Exception on registering MouseFun



Jacek Gajek
10-31-2009, 05:33 AM
In the following code:
("GLRenderableObject.cpp")

#include "GLRenderableObject.h"

bool GLRenderableObject::wasMouseFuncRegistered = false;
GLRenderableObject* GLRenderableObject::objects[256];
int GLRenderableObject::objectCount = 0;

GLRenderableObject::GLRenderableObject()
{
if (!wasMouseFuncRegistered)
{
wasMouseFuncRegistered = true;
objects[objectCount++] = this;
// Here I get an exception described below.
glutMouseFunc(StaticMouseFunction);
// If comment the above, the same exc appears too
glutMotionFunc(StaticMotionFunction);
}
}
void GLRenderableObject::StaticMouseFunction(int button, int state, int x, int y)
{
for (int i = 0; i < objectCount; i++)
objects[i]->OnMouseEvent(button, state, x, y);
}

void GLRenderableObject::StaticMotionFunction(int x, int y)
{
for (int i = 0; i < objectCount; i++)
objects[i]->OnMotion(x, y);
}
("GLRenderableObject.h")

#pragma once
#include "stdafx.h"

class GLRenderableObject
{
private:
static GLRenderableObject* objects[256];
static int objectCount;
static bool wasMouseFuncRegistered;
static void StaticMouseFunction(int button, int state, int x, int y);
static void StaticMotionFunction(int x, int y);


public:
GLRenderableObject();

virtual void Render() abstract {}

virtual void OnMouseEvent(int button, int state, int x, int y) {};
virtual void OnMotion(int x, int y) {};
};
I get "Unhandled exception at 0x100085fa in BransleysFern.exe: 0xC0000005: Access violation reading location 0x000000b0." on a line marked above. I tried to convert a static function to a global one but nothing changed.

Please help, I am a beginner.

I use OpenGL 3.1 + Visual Studio 2008 + C++ + glut.

DmitryM
10-31-2009, 06:52 AM
Do you call glutInit anywhere before glutMouseFunc?

Jacek Gajek
11-03-2009, 05:46 PM
I am sorry that I did not respond but I did not receive any notification about your reply. I have logged in to bump my message but suprise suprise there is a response.

Thanks for tip, I am checking if I do call glutInit right now.

Jacek Gajek
11-03-2009, 05:58 PM
There is a line

glutInitDisplayMode(GLUT_SINGLE | GLUT_RGBA);
in my code. I thought that it is all initialization I need, since all graphics is correct.
Anyway, now I added

void main(int argc, char **argv)
{
glutInit(&amp;argc, argv);
as well. The access violation error still appears. :(

Any other ideas? I can provide any additional info if needed.

DmitryM
11-03-2009, 06:48 PM
I don't see what's wrong, though haven't used glut very much.

I suggest you to take an existing example and change it iteratively into your program to know the exact step that breaks everything.

Jacek Gajek
11-04-2009, 04:14 AM
I got it. The glutMouseFunc and similar functions must be placed AFTER the

CreateWindow(..);
call.