GLFW + Angle: crash on glfwTerminate()

Hi,
so apparently I’ve found a bug between GLFW and Angle.
The angle examples terminate fine, so do the glfw examples.
The shaders I’ve used render fine and glGetString shows that indeed, Angle is being used.
The issue I’m having is that when calling glfwTerminate(), I get memory access violation (segfault) and a core dump.
I’ve run a gdb backtrace on it

#0  0x00007ffff72fef37 in _XSend () from /usr/lib/libX11.so.6
#1  0x00007ffff72f77d9 in XQueryExtension () from /usr/lib/libX11.so.6
#2  0x00007ffff6de7dd8 in ?? () from /usr/lib/libGLX.so.0
#3  0x00007ffff6dea2f9 in ?? () from /usr/lib/libGLX.so.0
#4  0x00007ffff6dea799 in glXDestroyPbuffer () from /usr/lib/libGLX.so.0
#5  0x00007ffff79672b7 in rx::DisplayGLX::terminate() ()
   from /home/a/libraries/angle/out/Release/libGLESv2.so
#6  0x00007ffff77bbd90 in egl::Display::terminate(egl::Thread*, egl::Display::TerminateReason) ()
   from /home/a/libraries/angle/out/Release/libGLESv2.so
#7  0x00007ffff760fb92 in egl::Terminate(egl::Thread*, egl::Display*) ()
   from /home/a/libraries/angle/out/Release/libGLESv2.so
#8  0x00007ffff7611678 in EGL_Terminate () from /home/a/libraries/angle/out/Release/libGLESv2.so
#9  0x00007ffff7c73ec2 in eglTerminate () from /home/a/libraries/angle/out/Release/libEGL.so
#10 0x000055555559cc93 in _glfwTerminateEGL ()
#11 0x000055555558e53a in _glfwTerminateX11 ()
#12 0x000055555557be24 in terminate ()
#13 0x000055555557cea8 in glfwTerminate ()
#14 0x000055555557a083 in shutdown () at esshader.c:503
#15 0x000055555557a785 in main (argc=1, argv=0x7fffffffe2b8) at esshader.c:722

Running glfwDestroyWindow(window); beforehand doesn’t change anything.
I’m using GLFW to load Angle:

#define GLFW_INCLUDE_ES3
#include <GLFW/glfw3.h>

And to initialize I’m doing this:

	if (!glfwInit()) {
		die("Unable to initialize GLFW.\n");
	}

	glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
	glfwWindowHint(GLFW_CONTEXT_CREATION_API, GLFW_EGL_CONTEXT_API);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
	glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 0);

	if (!(window = glfwCreateWindow(width, height, "esshader", monitor, NULL))) {
		glfwTerminate();
		die("Unable to create GLFW window.\n");
	}

	glfwMakeContextCurrent(window);

Maybe I’m doing something wrong again?

I suspect this is a problem: glfw/src/x11_init.c at 3eaf1255b29fdf5c2895856c7be7d7185ef2b241 · glfw/glfw (github.com)

That seems to be added to handle issue in nvidia EGL: X11: Fix segfault when using NVidia EGL · glfw/glfw@9e6c0c7 (github.com)
They require EGL to terminate after XCloseDisplay. But in Angle case it seems it probably wants display be active?

My god, again you’re spot-on!
Moving the line _glfwTerminateEGL(); from l1643 back to l1590 it now terminates just fine!
Sadly we can’t just use an #if defined here, it has to be a runtime check, maybe using glGetString to determine whether GPU is ANGLE / not NVIDIA?
That might be too hacky through…

I’m not sure what would be good way to solve that, have not been using nvidia on linux for a long time.
You should submit this to glfw issues on github, so this is at least known issue.

But if you’re not making library or plugin then there is a simple workaround - to not free anything on application exit. Don’t call glfwTerminate. Library/OS shutdown code will free all the resources for you automatically.

again, thank you very much @mmozeiko
If I understand correctly, not freeing anything will leave the OS to de-allocate all remaining memory the program has, I guess that’s one way to solve it :smiley:
But eventually, my goal is to use ANGLE in an audio plugin (VST3) and glfwTerminate would defo be a command important enough to have work.
I’ve now made an issue on the glfw github, maybe someone has a good idea.