diff --git a/src/cocoa_monitor.m b/src/cocoa_monitor.m index 2a7eaa6e..34224c3b 100644 --- a/src/cocoa_monitor.m +++ b/src/cocoa_monitor.m @@ -307,6 +307,8 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) _glfwInputError(GLFW_PLATFORM_ERROR, "Cocoa: Failed to find NSScreen for CGDisplay %s", monitors[i]->name); + + free(monitors); return NULL; } } diff --git a/src/init.c b/src/init.c index 97d605ca..588364c9 100644 --- a/src/init.c +++ b/src/init.c @@ -131,7 +131,7 @@ GLFWAPI int glfwInit(void) } _glfw.monitors = _glfwPlatformGetMonitors(&_glfw.monitorCount); - if (_glfw.monitors == NULL || _glfw.monitorCount == 0) + if (_glfw.monitors == NULL) { _glfwErrorCallback(GLFW_PLATFORM_ERROR, "No monitors found"); _glfwPlatformTerminate(); diff --git a/src/win32_monitor.c b/src/win32_monitor.c index 9bcd97db..47b5680d 100644 --- a/src/win32_monitor.c +++ b/src/win32_monitor.c @@ -107,6 +107,8 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) DWORD adapterIndex = 0; int primaryIndex = 0; + *count = 0; + for (;;) { DISPLAY_DEVICE adapter, display; @@ -152,6 +154,8 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* count) _glfwDestroyMonitors(monitors, found); _glfwInputError(GLFW_PLATFORM_ERROR, "Failed to convert string to UTF-8"); + + free(monitors); return NULL; } diff --git a/src/x11_monitor.c b/src/x11_monitor.c index a5bac66d..5e6ac851 100644 --- a/src/x11_monitor.c +++ b/src/x11_monitor.c @@ -162,15 +162,15 @@ void _glfwRestoreVideoMode(_GLFWmonitor* monitor) ////// GLFW platform API ////// ////////////////////////////////////////////////////////////////////////// -_GLFWmonitor** _glfwPlatformGetMonitors(int* found) +_GLFWmonitor** _glfwPlatformGetMonitors(int* count) { _GLFWmonitor** monitors = NULL; - *found = 0; + *count = 0; if (_glfw.x11.randr.available) { - int i; + int i, found = 0; RROutput primary; XRRScreenResources* sr; @@ -206,21 +206,21 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* found) continue; } - monitors[*found] = _glfwCreateMonitor(oi->name, - oi->mm_width, oi->mm_height); + monitors[found] = _glfwCreateMonitor(oi->name, + oi->mm_width, oi->mm_height); - monitors[*found]->x11.output = output; - monitors[*found]->x11.crtc = oi->crtc; + monitors[found]->x11.output = output; + monitors[found]->x11.crtc = oi->crtc; XRRFreeOutputInfo(oi); XRRFreeCrtcInfo(ci); - (*found)++; + found++; } XRRFreeScreenResources(sr); - for (i = 0; i < *found; i++) + for (i = 0; i < found; i++) { if (monitors[i]->x11.output == primary) { @@ -230,6 +230,14 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* found) break; } } + + if (found == 0) + { + free(monitors); + monitors = NULL; + } + + *count = found; } else { @@ -239,7 +247,7 @@ _GLFWmonitor** _glfwPlatformGetMonitors(int* found) _glfw.x11.screen), DisplayHeightMM(_glfw.x11.display, _glfw.x11.screen)); - *found = 1; + *count = 1; } return monitors;