GLFW crashes when an event happens window

I’m having a problem where my app crashes when the mouse enters the window I am using ImGui with multi view ports and the ImGui platform window works fine so i don’t think its GLFW breaking, its probably me making a mistake because I’ve only just started using the library. here is a link to my code. I am using MSYS2 MinGW 32 bit on window 11

Debugger helps figuring out why code crashes in these kind of situations. Running program under debugger will immediately show location of crash - this line: Pentagram/PentagramHeaders/windows.hpp at 6c01c5f7750b45f594da7bc7be9c31fbcd108b1a · ShoweryCellar34/Pentagram · GitHub

at() method throws exception when instanceList does not contain window you’re passing as argument.

And that happens because you’re using uninitialized window variable on this line: Pentagram/PentagramHeaders/windows.hpp at 6c01c5f7750b45f594da7bc7be9c31fbcd108b1a · ShoweryCellar34/Pentagram · GitHub

You can simplify code by not using std::unordered_map. GLFW allows to associate arbitrary pointer with GLFWwindow. This is done with glfwSetWindowUserPointer. Pass this as argument. Then later in callback you can get back this pointer with glfwGetWindowUserPointer. Like this:

Window(...) // constructor
{
    ...
    window = glfwCreateWindow(...);
    glfwSetWindowUserPointer(window, this);
    ...
}

void callbackManagers::cursorposCallbackManager(GLFWwindow* glfwWindow, double xpos, double ypos) {
    Window* window = static_cast<Window*>(glfwGetWindowUserPointer(glfwWindow));
    if(!window->IO->WantCaptureMouse) {window->data.userEventCallback(window, createCursorposEvent(xpos, ypos));}
}
1 Like

Thank you so much for this solution.