Results 1 to 6 of 6

Thread: Access Violation Exception on registering MouseFun

  1. #1
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    4

    Access Violation Exception on registering MouseFun

    In the following code:
    ("GLRenderableObject.cpp")
    Code :
    #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")
    Code :
    #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.

  2. #2
    Senior Member Regular Contributor DmitryM's Avatar
    Join Date
    Mar 2009
    Location
    Toronto
    Posts
    435

    Re: Access Violation Exception on registering MouseFun

    Do you call glutInit anywhere before glutMouseFunc?

  3. #3
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    4

    Re: Access Violation Exception on registering MouseFun

    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.

  4. #4
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    4

    Re: Access Violation Exception on registering MouseFun

    There is a line
    Code :
    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
    Code :
    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.

  5. #5
    Senior Member Regular Contributor DmitryM's Avatar
    Join Date
    Mar 2009
    Location
    Toronto
    Posts
    435

    Re: Access Violation Exception on registering MouseFun

    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.

  6. #6
    Junior Member Newbie
    Join Date
    Oct 2009
    Posts
    4

    Re: Access Violation Exception on registering MouseFun

    I got it. The glutMouseFunc and similar functions must be placed AFTER the
    Code :
    CreateWindow(..);
    call.

Similar Threads

  1. Access violation Exception with DrawElements
    By WorldDj in forum OpenGL: Basic Coding
    Replies: 0
    Last Post: 02-22-2018, 09:19 AM
  2. glCreateBuffers Access Violation Exception GLFW/GLEW
    By rackoon in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 03-07-2017, 02:18 AM
  3. Access Violation Exception [VC++]
    By Zababa! xD in forum OpenGL: Basic Coding
    Replies: 4
    Last Post: 11-02-2011, 09:17 PM
  4. Unhandled exception : Access Violation
    By Al Grant in forum OpenGL: Basic Coding
    Replies: 5
    Last Post: 07-06-2006, 01:27 PM
  5. Replies: 8
    Last Post: 04-26-2003, 10:35 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Proudly hosted by Digital Ocean