From 0c090a8c8b9ba75784bb42e52973dbf2b85a1bca Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Camilla=20L=C3=B6wy?= Date: Mon, 6 Feb 2017 16:03:49 +0100 Subject: [PATCH] Perform full termination on platform init failure --- src/init.c | 61 ++++++++++++++++++++++++++++++------------------------ 1 file changed, 34 insertions(+), 27 deletions(-) diff --git a/src/init.c b/src/init.c index 85148379..63f2fb86 100644 --- a/src/init.c +++ b/src/init.c @@ -77,6 +77,38 @@ static const char* getErrorString(int error) } } +// Terminate the library +// +static void terminate(void) +{ + int i; + + memset(&_glfw.callbacks, 0, sizeof(_glfw.callbacks)); + + while (_glfw.windowListHead) + glfwDestroyWindow((GLFWwindow*) _glfw.windowListHead); + + while (_glfw.cursorListHead) + glfwDestroyCursor((GLFWcursor*) _glfw.cursorListHead); + + for (i = 0; i < _glfw.monitorCount; i++) + { + _GLFWmonitor* monitor = _glfw.monitors[i]; + if (monitor->originalRamp.size) + _glfwPlatformSetGammaRamp(monitor, &monitor->originalRamp); + _glfwFreeMonitor(monitor); + } + + free(_glfw.monitors); + _glfw.monitors = NULL; + _glfw.monitorCount = 0; + + _glfwTerminateVulkan(); + _glfwPlatformTerminate(); + + memset(&_glfw, 0, sizeof(_glfw)); +} + ////////////////////////////////////////////////////////////////////////// ////// GLFW event API ////// @@ -124,7 +156,7 @@ GLFWAPI int glfwInit(void) if (!_glfwPlatformInit()) { - _glfwPlatformTerminate(); + terminate(); return GLFW_FALSE; } @@ -139,35 +171,10 @@ GLFWAPI int glfwInit(void) GLFWAPI void glfwTerminate(void) { - int i; - if (!_glfw.initialized) return; - memset(&_glfw.callbacks, 0, sizeof(_glfw.callbacks)); - - while (_glfw.windowListHead) - glfwDestroyWindow((GLFWwindow*) _glfw.windowListHead); - - while (_glfw.cursorListHead) - glfwDestroyCursor((GLFWcursor*) _glfw.cursorListHead); - - for (i = 0; i < _glfw.monitorCount; i++) - { - _GLFWmonitor* monitor = _glfw.monitors[i]; - if (monitor->originalRamp.size) - _glfwPlatformSetGammaRamp(monitor, &monitor->originalRamp); - _glfwFreeMonitor(monitor); - } - - free(_glfw.monitors); - _glfw.monitors = NULL; - _glfw.monitorCount = 0; - - _glfwTerminateVulkan(); - _glfwPlatformTerminate(); - - memset(&_glfw, 0, sizeof(_glfw)); + terminate(); } GLFWAPI void glfwGetVersion(int* major, int* minor, int* rev)