Fullscreen, unhandled exceptions and Visual Studio debugger

Good evening,
I have a problem that isn’t really a bug, but a strange behavior.
Usually when I launch an application from inside Visual Studio and an unhandled exception is thrown, the Visual Studio window is immediately focused and it shows the line that threw the exception.

Now if I initialize a fullscreen window with GLFW on Windows 10 (be it windowed fullscreen or real fullscreen) and an exception is thrown, the Visual Studio window isn’t focused and I can’t change window to see what Visual Studio says. It 's like the fullscreen window is istantly refocused after I try to change window. I can’t even open the Task Manager. The only way to exit from the application is to sign out from the window session and login again.
With a non-fullscreen window the problem doesn’t happen.

Is there any way to get around the problem? I’d like to keep using the comfortable Visual Studio feature that istantly show the origin of unhandled exceptions.

Minimum example (should be compiled an run from Visual Studio 2017):

#include <iostream>
#include <GLFW/glfw3.h>

int main()
{
	if (!glfwInit()) { throw std::runtime_error("GLFW initialization failed"); }

	glfwSetErrorCallback([](int error, char const* description)
		{
			std::cout << "GLWF error " << error << ": " << description << std::endl;
		});

	GLFWwindow * window = glfwCreateWindow(1920, 1080, "GLFW_test", glfwGetPrimaryMonitor(), nullptr);
	if (!window) { throw std::runtime_error("Window or OpenGL context creation failed"); }

	glfwSetKeyCallback(window, 
		[](GLFWwindow * window, int key, int scancode, int action, int mods)
		{
			if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
				glfwSetWindowShouldClose(window, GLFW_TRUE);
		});


	glfwMakeContextCurrent(window);
	glfwSwapInterval(1);

	while (!glfwWindowShouldClose(window))
	{
		static auto ticks = 0;
		++ticks;

		if (ticks > 200)
			throw std::runtime_error("Test");	// Unhandled exception test

		glfwSwapBuffers(window);
		glfwPollEvents();
	}

	glfwDestroyWindow(window);
	glfwTerminate();
}

This is a typical problem with Windows fullscreen applications - they are topmost and have focus even when they’ve hit a debug breakpoint or assert. This is one of the reasons many game and rendering developers use two monitors as Visual Studio can be kept in focus on the other monitor.

It is possible to switch to Visual Studio (which then goes back into the background) and then use keyboard shortcuts to exit the debug session and kill the app, though you’ll need to practice it to be effective.

1 Like

Thank you very much, your solution works fine.

I was surprised by this behavior because when I was using SFML to manage the window the Visual Studio debugger was working fine. Maybe it’s something related to the specific implementation of GLFW, or maybe the SFML developers have found some hack to avoid the problem. I really don’t know.