Allow NULL argument to _glfwPlatformFreeModule

This matches the behavior of _glfw_free and the public API, simplifying
cleanup code.
This commit is contained in:
Camilla Löwy
2025-12-04 15:33:24 +01:00
parent b579ea6792
commit f0b2992b3a
12 changed files with 45 additions and 134 deletions
+2
View File
@@ -688,6 +688,8 @@ void _glfwTerminateCocoa(void)
_glfwTerminateEGL(); _glfwTerminateEGL();
_glfwTerminateOSMesa(); _glfwTerminateOSMesa();
memset(&_glfw.ns, 0, sizeof(_glfw.ns));
} // autoreleasepool } // autoreleasepool
} }
+1 -4
View File
@@ -332,13 +332,10 @@ static void destroyContextEGL(_GLFWwindow* window)
// as it will make XCloseDisplay segfault // as it will make XCloseDisplay segfault
if (_glfw.platform.platformID != GLFW_PLATFORM_X11 || if (_glfw.platform.platformID != GLFW_PLATFORM_X11 ||
window->context.client != GLFW_OPENGL_API) window->context.client != GLFW_OPENGL_API)
{
if (window->context.egl.client)
{ {
_glfwPlatformFreeModule(window->context.egl.client); _glfwPlatformFreeModule(window->context.egl.client);
window->context.egl.client = NULL; window->context.egl.client = NULL;
} }
}
if (window->context.egl.surface) if (window->context.egl.surface)
{ {
@@ -552,7 +549,7 @@ void _glfwTerminateEGL(void)
} }
// Free modules only after all wayland termination functions are called // 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); _glfwPlatformFreeModule(_glfw.egl.handle);
_glfw.egl.handle = NULL; _glfw.egl.handle = NULL;
-3
View File
@@ -429,11 +429,8 @@ void _glfwTerminateGLX(void)
// NOTE: This function must not call any X11 functions, as it is called // NOTE: This function must not call any X11 functions, as it is called
// after XCloseDisplay (see _glfwTerminateX11 for details) // after XCloseDisplay (see _glfwTerminateX11 for details)
if (_glfw.glx.handle)
{
_glfwPlatformFreeModule(_glfw.glx.handle); _glfwPlatformFreeModule(_glfw.glx.handle);
_glfw.glx.handle = NULL; _glfw.glx.handle = NULL;
}
} }
#define SET_ATTRIB(a, v) \ #define SET_ATTRIB(a, v) \
+1
View File
@@ -260,5 +260,6 @@ void _glfwTerminateNull(void)
free(_glfw.null.clipboardString); free(_glfw.null.clipboardString);
_glfwTerminateOSMesa(); _glfwTerminateOSMesa();
_glfwTerminateEGL(); _glfwTerminateEGL();
memset(&_glfw.null, 0, sizeof(_glfw.null));
} }
-3
View File
@@ -180,11 +180,8 @@ GLFWbool _glfwInitOSMesa(void)
void _glfwTerminateOSMesa(void) void _glfwTerminateOSMesa(void)
{ {
if (_glfw.osmesa.handle)
{
_glfwPlatformFreeModule(_glfw.osmesa.handle); _glfwPlatformFreeModule(_glfw.osmesa.handle);
_glfw.osmesa.handle = NULL; _glfw.osmesa.handle = NULL;
}
} }
#define SET_ATTRIB(a, v) \ #define SET_ATTRIB(a, v) \
+1
View File
@@ -41,6 +41,7 @@ void* _glfwPlatformLoadModule(const char* path)
void _glfwPlatformFreeModule(void* module) void _glfwPlatformFreeModule(void* module)
{ {
if (module)
dlclose(module); dlclose(module);
} }
+1 -1
View File
@@ -157,8 +157,8 @@ GLFWbool _glfwInitVulkan(int mode)
void _glfwTerminateVulkan(void) void _glfwTerminateVulkan(void)
{ {
if (_glfw.vk.handle)
_glfwPlatformFreeModule(_glfw.vk.handle); _glfwPlatformFreeModule(_glfw.vk.handle);
_glfw.vk.handle = NULL;
} }
const char* _glfwGetVulkanResultString(VkResult result) const char* _glfwGetVulkanResultString(VkResult result)
+1 -1
View File
@@ -521,8 +521,8 @@ GLFWbool _glfwInitWGL(void)
void _glfwTerminateWGL(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) \ #define SET_ATTRIB(a, v) \
+8 -24
View File
@@ -166,29 +166,6 @@ static GLFWbool loadLibraries(void)
return GLFW_TRUE; 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 // Create key code translation tables
// //
static void createKeyTables(void) static void createKeyTables(void)
@@ -721,7 +698,14 @@ void _glfwTerminateWin32(void)
_glfwTerminateEGL(); _glfwTerminateEGL();
_glfwTerminateOSMesa(); _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 #endif // _GLFW_WIN32
+1
View File
@@ -39,6 +39,7 @@ void* _glfwPlatformLoadModule(const char* path)
void _glfwPlatformFreeModule(void* module) void _glfwPlatformFreeModule(void* module)
{ {
if (module)
FreeLibrary((HMODULE) module); FreeLibrary((HMODULE) module);
} }
+2 -29
View File
@@ -984,43 +984,16 @@ void _glfwTerminateWayland(void)
// Free modules only after all Wayland termination functions are called // Free modules only after all Wayland termination functions are called
if (_glfw.egl.handle)
{
_glfwPlatformFreeModule(_glfw.egl.handle); _glfwPlatformFreeModule(_glfw.egl.handle);
_glfw.egl.handle = NULL;
}
if (_glfw.wl.libdecor.handle)
{
_glfwPlatformFreeModule(_glfw.wl.libdecor.handle); _glfwPlatformFreeModule(_glfw.wl.libdecor.handle);
_glfw.wl.libdecor.handle = NULL;
}
if (_glfw.wl.egl.handle)
{
_glfwPlatformFreeModule(_glfw.wl.egl.handle); _glfwPlatformFreeModule(_glfw.wl.egl.handle);
_glfw.wl.egl.handle = NULL;
}
if (_glfw.wl.xkb.handle)
{
_glfwPlatformFreeModule(_glfw.wl.xkb.handle); _glfwPlatformFreeModule(_glfw.wl.xkb.handle);
_glfw.wl.xkb.handle = NULL;
}
if (_glfw.wl.cursor.handle)
{
_glfwPlatformFreeModule(_glfw.wl.cursor.handle); _glfwPlatformFreeModule(_glfw.wl.cursor.handle);
_glfw.wl.cursor.handle = NULL;
}
if (_glfw.wl.client.handle)
{
_glfwPlatformFreeModule(_glfw.wl.client.handle); _glfwPlatformFreeModule(_glfw.wl.client.handle);
_glfw.wl.client.handle = NULL;
}
_glfw_free(_glfw.wl.clipboardString); _glfw_free(_glfw.wl.clipboardString);
memset(&_glfw.wl, 0, sizeof(_glfw.wl));
} }
#endif // _GLFW_WAYLAND #endif // _GLFW_WAYLAND
+10 -52
View File
@@ -1592,71 +1592,29 @@ void _glfwTerminateX11(void)
_glfw.x11.display = NULL; _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(); _glfwTerminateOSMesa();
// NOTE: These need to be unloaded after XCloseDisplay, as they register // NOTE: These need to be unloaded after XCloseDisplay, as they register
// cleanup callbacks that get called by that function // cleanup callbacks that get called by that function
_glfwTerminateEGL(); _glfwTerminateEGL();
_glfwTerminateGLX(); _glfwTerminateGLX();
if (_glfw.x11.xlib.handle) _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); _glfwPlatformFreeModule(_glfw.x11.xlib.handle);
_glfw.x11.xlib.handle = NULL;
}
if (_glfw.x11.emptyEventPipe[0] || _glfw.x11.emptyEventPipe[1]) if (_glfw.x11.emptyEventPipe[0] || _glfw.x11.emptyEventPipe[1])
{ {
close(_glfw.x11.emptyEventPipe[0]); close(_glfw.x11.emptyEventPipe[0]);
close(_glfw.x11.emptyEventPipe[1]); close(_glfw.x11.emptyEventPipe[1]);
} }
memset(&_glfw.x11, 0, sizeof(_glfw.x11));
} }
#endif // _GLFW_X11 #endif // _GLFW_X11