# Destroying window failure, glfwDestroyWindow doen't work

**URL:** https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319
**Category:** support
**Created:** [March 10, 2023, 11:17am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319 "2023-03-10T11:17:09Z")
**Posts on this page:** 9
**Page:** 1

<div class="post-metadata">

### Author: ![RuiJing](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@RuiJing](https://discourse.glfw.org/u/RuiJing)
#### Post date: [March 10, 2023, 11:17am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/1 "2023-03-10T11:17:09Z")

</div>

When I want to release context, I use _glfwDestroyWindow (myWindow)_. This worked on my old computer, win10, nvidia 1080Ti. However, it didn’t work on my new notebook, win11, nvidia rtx 2050, visual studio 2022. I also tried to set window-close flag by calling _glfwSetWindowShouldClose (myWindow, TRUE)_, but when I check the flag by calling _BOOL temp = glfwWindowShouldClose (myWindow)_, I got a FALSE, which was quite unreasonable. Is it because my new notebook installed a win11 or nvidia rtx2050 ?

---

<div class="post-metadata">

### Author: ![ws909](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/ws909/32/714_2.png) [@ws909](https://discourse.glfw.org/u/ws909)
#### Post date: [March 10, 2023, 11:55am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/2 "2023-03-10T11:55:04Z")

</div>

This sounds like a bug in GLFW. Do you have a minimal example program? Does this happen in the GLFW test or example programs?

Have you set an error callback that prints errors in GLFW?

---

<div class="post-metadata">

### Author: ![RuiJing](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@RuiJing](https://discourse.glfw.org/u/RuiJing)
#### Post date: [March 11, 2023, 12:32pm UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/3 "2023-03-11T12:32:46Z")

</div>

I find the reason now. It’s the Nvidia GPU driver setting. On my old computer, I set the ‘thread optimization’ as ‘NO’, but on this new computer, I have to set it as ‘Auto’, otherwise, the render threads in my program will run in chaos.

---

<div class="post-metadata">

### Author: ![ws909](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/ws909/32/714_2.png) [@ws909](https://discourse.glfw.org/u/ws909)
#### Post date: [March 11, 2023, 8:49pm UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/4 "2023-03-11T20:49:31Z")

</div>

That’s peculiar. What does that functionality do? I’m not sure if I understand how that can affect something like `glfwWindowShouldClose` returning `GLFW_FALSE` after doing `glfwSetWindowShouldClose(..., GLFW_TRUE)`.

… well, unless you’re calling these from multiple threads, thereby not following the requirements of GLFW whatsoever.

---

<div class="post-metadata">

### Author: ![RuiJing](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@RuiJing](https://discourse.glfw.org/u/RuiJing)
#### Post date: [March 13, 2023, 9:44am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/5 "2023-03-13T09:44:58Z")

</div>

Yes, I used glfw in a multithreading program

---

<div class="post-metadata">

### Author: ![ws909](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/ws909/32/714_2.png) [@ws909](https://discourse.glfw.org/u/ws909)
#### Post date: [March 13, 2023, 9:56am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/6 "2023-03-13T09:56:36Z")

</div>

Are you using GLFW correctly? Mostly all of the GLFW calls you make must run on the main thread.

Is [this](https://github.com/glfw/glfw/issues/2239) relevant?

---

<div class="post-metadata">

### Author: ![RuiJing](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@RuiJing](https://discourse.glfw.org/u/RuiJing)
#### Post date: [March 17, 2023, 7:37am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/7 "2023-03-17T07:37:55Z")

</div>

For each MFC thread (AfxBeginThread), I created a window and made it as a glfw context, then I rendered images (e.g., glClear, glfwSwapBuffers, …) in the context. After rendering, I released context for each thread and killed both threads.

---

<div class="post-metadata">

### Author: ![dougbinks](https://yyz1.discourse-cdn.com/flex035/user_avatar/discourse.glfw.org/dougbinks/32/39_2.png) [@dougbinks](https://discourse.glfw.org/u/dougbinks)
#### Post date: [March 17, 2023, 10:42am UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/8 "2023-03-17T10:42:36Z")

</div>

GLFW windows can only be created from the main thread, see:

[https://www.glfw.org/docs/3.3/intro\_guide.html#thread\_safety](https://www.glfw.org/docs/3.3/intro_guide.html#thread_safety)

In particular the sentences:

_The reference documentation for every GLFW function states whether it is limited to the main thread._

_Initialization, termination, event processing and the creation and destruction of windows, cursors and OpenGL and OpenGL ES contexts are all restricted to the main thread due to limitations of one or several platforms._

You can create all the windows you need from the main thread, and then call `glfwMakeContextCurrent` to make the windows context current on the thread you want to draw to that window from.

Note that **main thread** here means the thread which your program was started with - i.e. the one where `main()` was called.

---

<div class="post-metadata">

### Author: ![RuiJing](https://avatars.discourse-cdn.com/v4/letter/r/e9c0ed/32.png) [@RuiJing](https://discourse.glfw.org/u/RuiJing)
#### Post date: [March 21, 2023, 2:34pm UTC](https://discourse.glfw.org/t/destroying-window-failure-glfwdestroywindow-doent-work/2319/9 "2023-03-21T14:34:25Z")

</div>

Oh, I see. I’m so grateful for your reminding. Now I changed my codes as you suggested.
