# GLFW crashes when an event happens window

**URL:** <https://discourse.glfw.org/t/glfw-crashes-when-an-event-happens-window/2641>\
**Category:** support\
**Created:** [May 30, 2024, 1:42am UTC](https://discourse.glfw.org/t/glfw-crashes-when-an-event-happens-window/2641 "2024-05-30T01:42:21Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![ShoweryCellar34](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/showerycellar34/32/918_2.png) [@ShoweryCellar34](https://discourse.glfw.org/u/ShoweryCellar34)\
**Post date:** [May 30, 2024, 1:42am UTC](https://discourse.glfw.org/t/glfw-crashes-when-an-event-happens-window/2641/1 "2024-05-30T01:42:21Z")

</div>

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](https://github.com/ShoweryCellar34/Pentagram-Demo). I am using MSYS2 MinGW 32 bit on window 11

---

<div class="post-metadata">

**Author:** ![mmozeiko](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/mmozeiko/32/29_2.png) [@mmozeiko](https://discourse.glfw.org/u/mmozeiko)\
**Post date:** [May 30, 2024, 2:17am UTC](https://discourse.glfw.org/t/glfw-crashes-when-an-event-happens-window/2641/2 "2024-05-30T02:17:59Z")

</div>

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](https://github.com/ShoweryCellar34/Pentagram/blob/6c01c5f7750b45f594da7bc7be9c31fbcd108b1a/PentagramHeaders/windows.hpp#L226)

`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](https://github.com/ShoweryCellar34/Pentagram/blob/6c01c5f7750b45f594da7bc7be9c31fbcd108b1a/PentagramHeaders/windows.hpp#L48)

You can simplify code by not using std::unordered\_map. GLFW allows to associate arbitrary pointer with GLFWwindow. This is done with [glfwSetWindowUserPointer](https://www.glfw.org/docs/3.3/group__window.html#ga3d2fc6026e690ab31a13f78bc9fd3651). Pass `this` as argument. Then later in callback you can get back `this` pointer with [glfwGetWindowUserPointer](https://www.glfw.org/docs/3.3/group__window.html#gae77a4add0d2023ca21ff1443ced01653). Like this:

```auto
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));}
}

```

---

<div class="post-metadata">

**Author:** ![ShoweryCellar34](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/showerycellar34/32/918_2.png) [@ShoweryCellar34](https://discourse.glfw.org/u/ShoweryCellar34)\
**Post date:** [May 30, 2024, 2:23am UTC](https://discourse.glfw.org/t/glfw-crashes-when-an-event-happens-window/2641/3 "2024-05-30T02:23:16Z")

</div>

Thank you so much for this solution.
