GLFW Shared Contexts

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.

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.

This is not an issue in general.

Not usually.

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

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.

No, they should be the same.

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

// do not : glUseProgram(0);

// 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

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

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

1 Like

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