dglfwderelict3

Access Violation Error when accessing D object from D-to-C callback


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


Solution

  • 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.