diff --git a/src/cocoa_init.m b/src/cocoa_init.m index 15dc4ec4..531d8b45 100644 --- a/src/cocoa_init.m +++ b/src/cocoa_init.m @@ -688,6 +688,8 @@ void _glfwTerminateCocoa(void) _glfwTerminateEGL(); _glfwTerminateOSMesa(); + memset(&_glfw.ns, 0, sizeof(_glfw.ns)); + } // autoreleasepool } diff --git a/src/egl_context.c b/src/egl_context.c index 46b098f5..4e59d78b 100644 --- a/src/egl_context.c +++ b/src/egl_context.c @@ -333,11 +333,8 @@ static void destroyContextEGL(_GLFWwindow* window) if (_glfw.platform.platformID != GLFW_PLATFORM_X11 || window->context.client != GLFW_OPENGL_API) { - if (window->context.egl.client) - { - _glfwPlatformFreeModule(window->context.egl.client); - window->context.egl.client = NULL; - } + _glfwPlatformFreeModule(window->context.egl.client); + window->context.egl.client = NULL; } if (window->context.egl.surface) @@ -552,7 +549,7 @@ void _glfwTerminateEGL(void) } // Free modules only after all wayland termination functions are called - if (_glfw.egl.handle && _glfw.platform.platformID != GLFW_PLATFORM_WAYLAND) + if (_glfw.platform.platformID != GLFW_PLATFORM_WAYLAND) { _glfwPlatformFreeModule(_glfw.egl.handle); _glfw.egl.handle = NULL; diff --git a/src/glx_context.c b/src/glx_context.c index 84fca9dd..f08cdd5b 100644 --- a/src/glx_context.c +++ b/src/glx_context.c @@ -429,11 +429,8 @@ void _glfwTerminateGLX(void) // NOTE: This function must not call any X11 functions, as it is called // after XCloseDisplay (see _glfwTerminateX11 for details) - if (_glfw.glx.handle) - { - _glfwPlatformFreeModule(_glfw.glx.handle); - _glfw.glx.handle = NULL; - } + _glfwPlatformFreeModule(_glfw.glx.handle); + _glfw.glx.handle = NULL; } #define SET_ATTRIB(a, v) \ diff --git a/src/null_init.c b/src/null_init.c index 8c10f5e6..fd0d9e96 100644 --- a/src/null_init.c +++ b/src/null_init.c @@ -260,5 +260,6 @@ void _glfwTerminateNull(void) free(_glfw.null.clipboardString); _glfwTerminateOSMesa(); _glfwTerminateEGL(); + memset(&_glfw.null, 0, sizeof(_glfw.null)); } diff --git a/src/osmesa_context.c b/src/osmesa_context.c index 0bb375a8..c7a4c868 100644 --- a/src/osmesa_context.c +++ b/src/osmesa_context.c @@ -180,11 +180,8 @@ GLFWbool _glfwInitOSMesa(void) void _glfwTerminateOSMesa(void) { - if (_glfw.osmesa.handle) - { - _glfwPlatformFreeModule(_glfw.osmesa.handle); - _glfw.osmesa.handle = NULL; - } + _glfwPlatformFreeModule(_glfw.osmesa.handle); + _glfw.osmesa.handle = NULL; } #define SET_ATTRIB(a, v) \ diff --git a/src/posix_module.c b/src/posix_module.c index b3482d26..de99bc69 100644 --- a/src/posix_module.c +++ b/src/posix_module.c @@ -41,7 +41,8 @@ void* _glfwPlatformLoadModule(const char* path) void _glfwPlatformFreeModule(void* module) { - dlclose(module); + if (module) + dlclose(module); } GLFWproc _glfwPlatformGetModuleSymbol(void* module, const char* name) diff --git a/src/vulkan.c b/src/vulkan.c index 9c87fcfe..8f25f5ee 100644 --- a/src/vulkan.c +++ b/src/vulkan.c @@ -157,8 +157,8 @@ GLFWbool _glfwInitVulkan(int mode) void _glfwTerminateVulkan(void) { - if (_glfw.vk.handle) - _glfwPlatformFreeModule(_glfw.vk.handle); + _glfwPlatformFreeModule(_glfw.vk.handle); + _glfw.vk.handle = NULL; } const char* _glfwGetVulkanResultString(VkResult result) diff --git a/src/wgl_context.c b/src/wgl_context.c index 04429066..1fb7d322 100644 --- a/src/wgl_context.c +++ b/src/wgl_context.c @@ -521,8 +521,8 @@ GLFWbool _glfwInitWGL(void) void _glfwTerminateWGL(void) { - if (_glfw.wgl.instance) - _glfwPlatformFreeModule(_glfw.wgl.instance); + _glfwPlatformFreeModule(_glfw.wgl.instance); + _glfw.wgl.instance = NULL; } #define SET_ATTRIB(a, v) \ diff --git a/src/win32_init.c b/src/win32_init.c index 90e47670..d526fe8a 100644 --- a/src/win32_init.c +++ b/src/win32_init.c @@ -166,29 +166,6 @@ static GLFWbool loadLibraries(void) return GLFW_TRUE; } -// Unload used libraries (DLLs) -// -static void freeLibraries(void) -{ - if (_glfw.win32.xinput.instance) - _glfwPlatformFreeModule(_glfw.win32.xinput.instance); - - if (_glfw.win32.dinput8.instance) - _glfwPlatformFreeModule(_glfw.win32.dinput8.instance); - - if (_glfw.win32.user32.instance) - _glfwPlatformFreeModule(_glfw.win32.user32.instance); - - if (_glfw.win32.dwmapi.instance) - _glfwPlatformFreeModule(_glfw.win32.dwmapi.instance); - - if (_glfw.win32.shcore.instance) - _glfwPlatformFreeModule(_glfw.win32.shcore.instance); - - if (_glfw.win32.ntdll.instance) - _glfwPlatformFreeModule(_glfw.win32.ntdll.instance); -} - // Create key code translation tables // static void createKeyTables(void) @@ -721,7 +698,14 @@ void _glfwTerminateWin32(void) _glfwTerminateEGL(); _glfwTerminateOSMesa(); - freeLibraries(); + _glfwPlatformFreeModule(_glfw.win32.xinput.instance); + _glfwPlatformFreeModule(_glfw.win32.dinput8.instance); + _glfwPlatformFreeModule(_glfw.win32.user32.instance); + _glfwPlatformFreeModule(_glfw.win32.dwmapi.instance); + _glfwPlatformFreeModule(_glfw.win32.shcore.instance); + _glfwPlatformFreeModule(_glfw.win32.ntdll.instance); + + memset(&_glfw.win32, 0, sizeof(_glfw.win32)); } #endif // _GLFW_WIN32 diff --git a/src/win32_module.c b/src/win32_module.c index b76599b6..ca5ce9cf 100644 --- a/src/win32_module.c +++ b/src/win32_module.c @@ -39,7 +39,8 @@ void* _glfwPlatformLoadModule(const char* path) void _glfwPlatformFreeModule(void* module) { - FreeLibrary((HMODULE) module); + if (module) + FreeLibrary((HMODULE) module); } GLFWproc _glfwPlatformGetModuleSymbol(void* module, const char* name) diff --git a/src/wl_init.c b/src/wl_init.c index 025f7746..b2fd6a88 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -984,43 +984,16 @@ void _glfwTerminateWayland(void) // Free modules only after all Wayland termination functions are called - if (_glfw.egl.handle) - { - _glfwPlatformFreeModule(_glfw.egl.handle); - _glfw.egl.handle = NULL; - } - - if (_glfw.wl.libdecor.handle) - { - _glfwPlatformFreeModule(_glfw.wl.libdecor.handle); - _glfw.wl.libdecor.handle = NULL; - } - - if (_glfw.wl.egl.handle) - { - _glfwPlatformFreeModule(_glfw.wl.egl.handle); - _glfw.wl.egl.handle = NULL; - } - - if (_glfw.wl.xkb.handle) - { - _glfwPlatformFreeModule(_glfw.wl.xkb.handle); - _glfw.wl.xkb.handle = NULL; - } - - if (_glfw.wl.cursor.handle) - { - _glfwPlatformFreeModule(_glfw.wl.cursor.handle); - _glfw.wl.cursor.handle = NULL; - } - - if (_glfw.wl.client.handle) - { - _glfwPlatformFreeModule(_glfw.wl.client.handle); - _glfw.wl.client.handle = NULL; - } + _glfwPlatformFreeModule(_glfw.egl.handle); + _glfwPlatformFreeModule(_glfw.wl.libdecor.handle); + _glfwPlatformFreeModule(_glfw.wl.egl.handle); + _glfwPlatformFreeModule(_glfw.wl.xkb.handle); + _glfwPlatformFreeModule(_glfw.wl.cursor.handle); + _glfwPlatformFreeModule(_glfw.wl.client.handle); _glfw_free(_glfw.wl.clipboardString); + + memset(&_glfw.wl, 0, sizeof(_glfw.wl)); } #endif // _GLFW_WAYLAND diff --git a/src/x11_init.c b/src/x11_init.c index 5f760f43..b0acbf7f 100644 --- a/src/x11_init.c +++ b/src/x11_init.c @@ -1592,71 +1592,29 @@ void _glfwTerminateX11(void) _glfw.x11.display = NULL; } - if (_glfw.x11.x11xcb.handle) - { - _glfwPlatformFreeModule(_glfw.x11.x11xcb.handle); - _glfw.x11.x11xcb.handle = NULL; - } - - if (_glfw.x11.xcursor.handle) - { - _glfwPlatformFreeModule(_glfw.x11.xcursor.handle); - _glfw.x11.xcursor.handle = NULL; - } - - if (_glfw.x11.randr.handle) - { - _glfwPlatformFreeModule(_glfw.x11.randr.handle); - _glfw.x11.randr.handle = NULL; - } - - if (_glfw.x11.xinerama.handle) - { - _glfwPlatformFreeModule(_glfw.x11.xinerama.handle); - _glfw.x11.xinerama.handle = NULL; - } - - if (_glfw.x11.xrender.handle) - { - _glfwPlatformFreeModule(_glfw.x11.xrender.handle); - _glfw.x11.xrender.handle = NULL; - } - - if (_glfw.x11.xshape.handle) - { - _glfwPlatformFreeModule(_glfw.x11.xshape.handle); - _glfw.x11.xshape.handle = NULL; - } - - if (_glfw.x11.vidmode.handle) - { - _glfwPlatformFreeModule(_glfw.x11.vidmode.handle); - _glfw.x11.vidmode.handle = NULL; - } - - if (_glfw.x11.xi.handle) - { - _glfwPlatformFreeModule(_glfw.x11.xi.handle); - _glfw.x11.xi.handle = NULL; - } - _glfwTerminateOSMesa(); // NOTE: These need to be unloaded after XCloseDisplay, as they register // cleanup callbacks that get called by that function _glfwTerminateEGL(); _glfwTerminateGLX(); - if (_glfw.x11.xlib.handle) - { - _glfwPlatformFreeModule(_glfw.x11.xlib.handle); - _glfw.x11.xlib.handle = NULL; - } + _glfwPlatformFreeModule(_glfw.x11.x11xcb.handle); + _glfwPlatformFreeModule(_glfw.x11.xcursor.handle); + _glfwPlatformFreeModule(_glfw.x11.randr.handle); + _glfwPlatformFreeModule(_glfw.x11.xinerama.handle); + _glfwPlatformFreeModule(_glfw.x11.xrender.handle); + _glfwPlatformFreeModule(_glfw.x11.xshape.handle); + _glfwPlatformFreeModule(_glfw.x11.vidmode.handle); + _glfwPlatformFreeModule(_glfw.x11.xi.handle); + _glfwPlatformFreeModule(_glfw.x11.xlib.handle); if (_glfw.x11.emptyEventPipe[0] || _glfw.x11.emptyEventPipe[1]) { close(_glfw.x11.emptyEventPipe[0]); close(_glfw.x11.emptyEventPipe[1]); } + + memset(&_glfw.x11, 0, sizeof(_glfw.x11)); } #endif // _GLFW_X11