2

I've recently begun to dip my toes into DerelictGLFW. I have two classes, one of them a Window class, and another an InputHandler class (a event manager for window events). In my cursor position callback, I take the window user pointer and try to set the position, but I get an Access Violation Error immediately upon attempting to set any value outside of the callback and GLFW. GLFW is initialized, and does not report any errors. Thank you for your time.

Class Window
{
    private:
        double cursorX;

    ...other stuffs...

    @property
    void cursorX(double x) nothrow
    {
        cursorX = x;
    }
}

extern(C) void mousePosCallback(GLFWwindow* window, double x, double y)
{
    Window* _window = window.userPointer 
    //userPointer is a static function that gets the window user pointer
    //and casts it to a Window*

    _window.cursorX = x;
}

static Window* userPointer(GLFWwindow* window)
{
    return cast(Window*) glfwGetWindowUserPointer(window);
}

Edits:

Added extern(C) to callback, error persists.

Corrected "immediately upon entering the callback" to "immediately upon attempting to set any value outside of the callback and GLFW".

Added userPointer function to question

Straivers
  • 349
  • 1
  • 9
  • Run it in GDB and see if you can get a backtrace. Also show the code for `userPointer`. – Colonel Thirty Two Apr 19 '15 at 16:22
  • 1
    Also, 1) classes already have reference semantics so `Window*` is probably wrong, and 2) the window object will not be picked up by the GC and will likely be garbage collected. Consider making Window a struct at the least. – Colonel Thirty Two Apr 19 '15 at 16:24
  • Did you try to compile with -g? It should tell you exactly where the access violation is thrown. – Bauss Apr 19 '15 at 17:03
  • How is the user pointer set? It should be set with `cast(void*)window` and retrieved with `cast(Window)userptr`. – weltensturm Apr 21 '15 at 13:53

2 Answers2

1

mousePosCallback must be declared in a extern(C) block. This is to make the calling convention match.

extern (C) void mousePosCallback(GLFWwindow* window, double x, double y)
{
    Window* _window = window.userPointer 
    //userPointer is a static function that gets the window user pointer
    //and casts it to a Window*

    _window.cursorX = x;
}
ratchet freak
  • 47,288
  • 5
  • 68
  • 106
0

It seems I have discovered the source of the error. During initialization of the window, I attempt to set the user pointer with this. I'm not sure why, but moving it into a different function that is not called from the constructor appears to remedy the problem. The problem is solved, but could anyone help me to understand why? It seems rather odd to me.

Straivers
  • 349
  • 1
  • 9