Access Violation Exception on registering MouseFun

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;
[b]// Here I get an exception described below.[/b]
		glutMouseFunc(StaticMouseFunction);
[i]// If comment the above, the same exc appears too[/i]
		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.

Do you call glutInit anywhere before glutMouseFunc?

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.

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(&argc, argv);

as well. The access violation error still appears. :frowning:

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

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.

I got it. The glutMouseFunc and similar functions must be placed AFTER the

CreateWindow(..);

call.