# GLFW Shared Contexts

**URL:** https://discourse.glfw.org/t/glfw-shared-contexts/1707
**Category:** support
**Created:** [November 26, 2020, 12:03am UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707 "2020-11-26T00:03:05Z")
**Posts on this page:** 6
**Page:** 1

<div class="post-metadata">

### Author: ![stephane](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@stephane](https://discourse.glfw.org/u/stephane)
#### Post date: [November 26, 2020, 12:03am UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707/1 "2020-11-26T00:03:05Z")

</div>

Hi,  
I am currently working on my game framework (c++, GLFW 3.3.2, OpenGL 4.4).  
I would like to add to possibility to work with multiple windows. In that purpose, i start working with shared contexts.  
So i created a ‘useless’ window that is hidden and that just deserves to share its context with any new ‘real’ windows (call this window FAKE1)  
In my tests i created 2 other game windows as a proof of concept, sharing FAKE1 context (call them GAME1 and GAME2)  
At first, this works fine, but as soon as i close one of the 2 games window the display collapses.  
-I checked that before rendering GAME1 or GAME2 i set the concerned window’s context current  
-I checked that before creating GAME1 or GAME2, the FAKE1 context is not current (as mentionned in glfwCreateWindow documentation)  
-In my code, Textures et VertexBuffer are shared but not VertexArray (while such objects seem not to be sharable)  
-The code is not multithreaded  
The application i made is constantly allocating new resources. Is this an issue ?  
Does the active context when a texture/buffer is beeing created of any concern ?  
Must i always allocate resources with FAKE1 context ?  
Can i create resources with GAME1 context active and use it inside GAME2 ? destroy it inside GAME2 ?  
I am using glew to retreive OpenGL functions pointers. Can theses pointers be different between contexts ?

Thank you for your help.

---

<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: [November 26, 2020, 12:52pm UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707/2 "2020-11-26T12:52:50Z")

</div>

> [@stephane](#):
>
> At first, this works fine, but as soon as i close one of the 2 games window the display collapses.

GLFW itself does not close windows, it is your own code which calls `glfwDestroyWindow`. Are you accidentally closing all windows when `glfwWindowShouldClose` returns true for one?

In general a problem like this should be solved by debugging the code (with a debugger such as Visual Studio or GDB), or adding `printf` statements to check the code flow is as expected.

> [@stephane](#):
>
> The application i made is constantly allocating new resources. Is this an issue ?

This is not an issue in general.

> [@stephane](#):
>
> Does the active context when a texture/buffer is beeing created of any concern ?

Not usually.

> [@stephane](#):
>
> Must i always allocate resources with FAKE1 context ?

No, you can allocate shareable resources on any of the shared contexts.

> [@stephane](#):
>
> Can i create resources with GAME1 context active and use it inside GAME2 ? destroy it inside GAME2 ?

Yes, so long as the resource is not bound in GAME1 when destroyed in GAME2, though this usually just results in incorrect rendering rather than a crash due to most OpenGL drivers being fairly fault tolerant.

> [@stephane](#):
>
> I am using glew to retreive OpenGL functions pointers. Can theses pointers be different between contexts ?

No, they should be the same.

---

<div class="post-metadata">

### Author: ![stephane](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@stephane](https://discourse.glfw.org/u/stephane)
#### Post date: [November 26, 2020, 1:13pm UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707/3 "2020-11-26T13:13:03Z")

</div>

Thank you for your answers.  
Concerning the GAME window closure, i destroy a single window at a time with glfwDestroyWindow. The other remains alive but it becomes black or its rendering is corrupted.  
What thing i do not always make is unbind the resources after rendering in one context.  
for pseudo code example

```
// rendering
glfwMakeCurrentContext(my_game1);
glUseProgram(program_id);
glBindVertexArray(vertex_array_id);

glBindVertexArray(0);
// do not : glUseProgram(0);
glfwMakeCurrentContext(nullptr);

// destroying the window
// at this point the program (and its attributes) are still bound to the context being destroyed, 
// and i expect to be able to still use program_id in my_game2
glfwDestroyWindow(my_game1); 

```

So you are telling me that i should ensure that glUseProgram(0) is being called before destruction (and so on for other resources) ?

Thank you

---

<div class="post-metadata">

### Author: ![stephane](https://avatars.discourse-cdn.com/v4/letter/s/dfb087/32.png) [@stephane](https://discourse.glfw.org/u/stephane)
#### Post date: [November 29, 2020, 3:55pm UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707/4 "2020-11-29T15:55:05Z")

</div>

Thank for your help.  
It seems i found what caused my issue. I want to share that knowledge.

Vertex Array Objects (VAO) are not shared among shared context (I knew that) but i did not realized at first that two VAOs with same ID could exist at the same time (as soon as they belong to different contexts)

In my code there was 2 VAO with the same id. After having destroyed my window Game1, i was destroying such a VAO but with the Game2 context current. This was destroying the wrong resource for the wrong context.

I hope this will help

---

<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: [November 29, 2020, 4:44pm UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707/5 "2020-11-29T16:44:54Z")

</div>

Glad you found the issue, and thanks for posting the answer - that should help folk with similar problems!

---

<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: [September 6, 2026, 6:08pm UTC](https://discourse.glfw.org/t/glfw-shared-contexts/1707/6 "2026-09-06T18:08:05Z")

</div>

A post was split to a new topic: [Can you please explain what context sharing then means?](https://discourse.glfw.org/t/can-you-please-explain-what-context-sharing-then-means/3023)
