# glfwCreateWindow hangs when creating fullscreen window on secondary monitor

**URL:** <https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878>\
**Category:** support\
**Created:** [March 17, 2017, 2:26am UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878 "2017-03-17T02:26:35Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![efpkop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@efpkop](https://discourse.glfw.org/u/efpkop)\
**Post date:** [March 17, 2017, 2:26am UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/1 "2017-03-17T02:26:35Z")

</div>

I’m trying to create a full screen render window on a secondary monitor. The following is a relatively simple example that exhibits the error. When I set monitor\_num to anything but 0 (i.e. my main/primary display) my code hangs at the glfwCreateWindow line. What am I doing wrong?

I’m operating on a Windows 10, 64-bit system with an Nvidia GPU and Visual Studio 2013.

```c
#include <glad/glad.h>
#include <GLFW/glfw3.h>

#include <stdlib.h>
#include <stdio.h>

GLuint programID;
GLuint vao;
GLuint vertexbuffer;

#define monitor_num 1

static void error_callback(int error, const char* description)
{
	fputs(description, stderr);
}
static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
{
	if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
		glfwSetWindowShouldClose(window, GL_TRUE);
}
void setup_shaders(void);
int main(void)
{
	GLFWwindow* window;
	glfwSetErrorCallback(error_callback);
	if (!glfwInit())
		exit(EXIT_FAILURE);

	int number_of_monitors;
	GLFWmonitor** monitors = glfwGetMonitors(&number_of_monitors);
	const GLFWvidmode * current_mode = glfwGetVideoMode(monitors[monitor_num]);

	glfwWindowHint(GLFW_MAXIMIZED, GL_TRUE);
	glfwWindowHint(GLFW_AUTO_ICONIFY, GL_FALSE);
	window = glfwCreateWindow(current_mode->width, current_mode->height, "Simple example", monitors[monitor_num], NULL);
	if (!window)
	{
		glfwTerminate();
		exit(EXIT_FAILURE);
	}
	glfwMakeContextCurrent(window);
	if (!gladLoadGLLoader((GLADloadproc)glfwGetProcAddress))
	{
		return false;
	}
	glfwSetKeyCallback(window, key_callback);

	glGenVertexArrays(1, &vao);
	glBindVertexArray(vao);
	setup_shaders();
	static const GLfloat g_vertex_buffer_data[] = 
	{
		-0.6f, -0.4f, 0.f, 
		 0.6f, -0.4f, 0.f,
		 0.f, 0.6f, 0.f,
	};

	glGenBuffers(1, &vertexbuffer);
	glBindBuffer(GL_ARRAY_BUFFER, vertexbuffer);
	glBufferData(GL_ARRAY_BUFFER, sizeof(g_vertex_buffer_data), g_vertex_buffer_data, GL_STATIC_DRAW);
	glVertexAttribPointer(0, 3, GL_FLOAT, GL_FALSE, 0, (void*)0);
	glEnableVertexAttribArray(0);
	glBindBuffer(GL_ARRAY_BUFFER, 0);
	glBindVertexArray(0);
	glUseProgram(0);

	while (!glfwWindowShouldClose(window))
	{
		glUseProgram(programID);
		glBindVertexArray(vao);
		glEnableVertexAttribArray(0);
		glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 
		glDrawArrays(GL_TRIANGLES, 0, 3); 
		glDisableVertexAttribArray(0);
		glBindVertexArray(0);
		glUseProgram(0);
		glfwSwapBuffers(window);
		glfwPollEvents();
	}
	glfwDestroyWindow(window);
	glfwTerminate();
	exit(EXIT_SUCCESS);
}

void setup_shaders(void)
{
	static const char * gl_vertex_shader_source_code[] =
	{
		"#version 330 core \n"
		" \n"
		"layout(location = 0) in vec3 vertexPosition_modelspace; \n"
		" \n"
		"void main(){ \n"
		" \n"
		"	gl_Position = vec4(vertexPosition_modelspace, 1); \n"
		"} \n"
	};

	static const GLchar * gl_fragment_shader_source_code[] =
	{
		"#version 330 core \n"
		" \n"
		"out vec3 color; \n" 
		" \n"
		"void main() \n"
		"{ \n"
		"	color = vec3(0.5, 1.0, 0.8); \n" 
		"} \n"
	};

	GLuint vertex_shader = glCreateShader(GL_VERTEX_SHADER);
	glShaderSource(vertex_shader, 1, gl_vertex_shader_source_code, NULL);
	glCompileShader(vertex_shader);

	GLuint fragment_shader = glCreateShader(GL_FRAGMENT_SHADER);
	glShaderSource(fragment_shader, 1, gl_fragment_shader_source_code, NULL);
	glCompileShader(fragment_shader);

	programID = glCreateProgram();
	glAttachShader(programID, vertex_shader);
	glAttachShader(programID, fragment_shader);
	glLinkProgram(programID);

	glDeleteShader(vertex_shader);
	glDeleteShader(fragment_shader);
}

```

---

<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, 2017, 5:40pm UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/2 "2017-03-17T17:40:34Z")

</div>

Have you checked that number\_of\_monitors \> monitor\_num?

---

<div class="post-metadata">

**Author:** ![efpkop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@efpkop](https://discourse.glfw.org/u/efpkop)\
**Post date:** [March 17, 2017, 7:25pm UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/3 "2017-03-17T19:25:20Z")

</div>

I have three monitors connected (& number\_of\_monitors does report back as 3). So, in this case, when monitor\_num == 0, it works. But when monitor\_num == 1 or 2, it hangs.

---

<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, 2017, 7:36pm UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/4 "2017-03-17T19:36:40Z")

</div>

Are you able to debug the code and find out where it hangs?

Additionally have you tested starting the app windowed, then switching fullscreen to that monitor?

---

<div class="post-metadata">

**Author:** ![efpkop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@efpkop](https://discourse.glfw.org/u/efpkop)\
**Post date:** [March 19, 2017, 3:15am UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/5 "2017-03-19T03:15:31Z")

</div>

Yes - I was debugging the code all along and it was hanging at the glfwCreateWindow command.

However, as you suggested, if I create a windowed version first and then call glfwSetWindowMonitor, I am able to switch to fullscreen mode just fine. That appears to have solved the problem.

Thanks.

---

<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 19, 2017, 9:24am UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/6 "2017-03-19T09:24:05Z")

</div>

There could be a bug here [which may need reporting](https://github.com/glfw/glfw/issues) but it might be difficult for the maintainers to investigate if they can’t reproduce.

If you have a little time to dig further, you could try this with the latest code on github and if the problem persists attempt to find out where in the glfw code it is hanging. To make sure the debugger itself isn’t causing the hang (debugging fullscreen apps can cause issues), you might need to start with the debugger detached and then attach and pause when the problem occurs.

---

<div class="post-metadata">

**Author:** ![efpkop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@efpkop](https://discourse.glfw.org/u/efpkop)\
**Post date:** [March 20, 2017, 7:03pm UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/7 "2017-03-20T19:03:13Z")

</div>

I am running GLFW v3.2.1 which, on github, is reported as the latest stable release.

Unfortunately, I don’t have a lot of time to debug the latest GLFW source. That’s why I tried to create the example code above which represents the simplest set of commands using modern OpenGL that is demonstrating this error. I would be curious if anyone else can run this code and show similar behavior. (I have been able to get the same failure on two different systems - one running Visual Studio 2015 and one running Visual Studio 2013).

I wish I had time to debug the GLFW source (but I’m not even sure how to detach and reattach the debugger mid-program, as you suggest!). That being said, I just downloaded the latest GLFW source (version 3.3). Generated a visual studio project w/ CMake and implemented the above code. Here’s my trace of where the bug is occurring:

```
 glfwCreateWindow
      ===> __glfwRefreshContextAttribs(&ctxconfig) (@ line 212 of window.c)
               ===> window->context.swapBuffers(window); (@ line 541 of context.c)
                         ===> SwapBuffers(window->context.wgl.dc); (@ line 266 of wgl_context.c)

```

I can’t proceed any deeper than this function. The code just hangs at the above line.

As an aside, I am seeing another odd artifact when using the glfwSetWindowMonitor command as described above. The window will open up full-screen in the desired monitor - but the viewport coordinates are scaled incorrectly. For example, if I create a triangle whose vertices should touch two corners and the top center of the screen, it will display correctly if I go full-screen on my main display - but if I transfer to either my secondary monitors, the triangle will be mis-scaled (hanging off the side of the screen.) I’m going to try to debug that on my own. I will probably start a different question thread on that issue if I can’t figure it out.

---

<div class="post-metadata">

**Author:** ![efpkop](https://avatars.discourse-cdn.com/v4/letter/e/e95f7d/32.png) [@efpkop](https://discourse.glfw.org/u/efpkop)\
**Post date:** [March 21, 2017, 12:32am UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/8 "2017-03-21T00:32:32Z")

</div>

Quick update: My last observation of said ‘odd artifact’ is corrected with a call to:  
glViewport(0, 0, current\_mode-\>width, current\_mode-\>height);

When you switch over to the secondary monitor, the original (main) display’s viewport is still locked in.

---

<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 22, 2017, 7:14pm UTC](https://discourse.glfw.org/t/glfwcreatewindow-hangs-when-creating-fullscreen-window-on-secondary-monitor/878/9 "2017-03-22T19:14:56Z")

</div>

GLFW doesn’t have a test for starting fullscreen on different monitors, but I recently added the ability to do so on my own project [Avoyd](https://www.enkisoftware.com/) in version 0.1.0.152 which you can download for free to see if this is a GLFW issue or something in your code.

If you select Resize-\>Fullscreen Options you can choose which monitor and resolution to go fullscreen on. If you then close Avoyd when in fullscreen and restart it should restart on the same monitor, if not there’s a problem and I’ll try to get you some test code with extra debug information.
