From 8958530eb9bdb2c608c82c00b1744db357202fb0 Mon Sep 17 00:00:00 2001 From: Doug Binks Date: Tue, 4 Nov 2025 09:38:28 +0000 Subject: [PATCH] Wayland: _glfwPlatformFreeModule calls moved to end - Potential resolution for issue #2744 _glfwTerminateWayland segfault --- src/wl_init.c | 46 ++++++++++++++++++++++++---------------------- 1 file changed, 24 insertions(+), 22 deletions(-) diff --git a/src/wl_init.c b/src/wl_init.c index ef9e45036..ff7b2869a 100644 --- a/src/wl_init.c +++ b/src/wl_init.c @@ -907,18 +907,6 @@ void _glfwTerminateWayland(void) libdecor_unref(_glfw.wl.libdecor.context); } - 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.composeState) xkb_compose_state_unref(_glfw.wl.xkb.composeState); if (_glfw.wl.xkb.keymap) @@ -927,21 +915,11 @@ void _glfwTerminateWayland(void) xkb_state_unref(_glfw.wl.xkb.state); if (_glfw.wl.xkb.context) xkb_context_unref(_glfw.wl.xkb.context); - if (_glfw.wl.xkb.handle) - { - _glfwPlatformFreeModule(_glfw.wl.xkb.handle); - _glfw.wl.xkb.handle = NULL; - } if (_glfw.wl.cursorTheme) wl_cursor_theme_destroy(_glfw.wl.cursorTheme); if (_glfw.wl.cursorThemeHiDPI) wl_cursor_theme_destroy(_glfw.wl.cursorThemeHiDPI); - if (_glfw.wl.cursor.handle) - { - _glfwPlatformFreeModule(_glfw.wl.cursor.handle); - _glfw.wl.cursor.handle = NULL; - } for (unsigned int i = 0; i < _glfw.wl.offerCount; i++) wl_data_offer_destroy(_glfw.wl.offers[i].offer); @@ -1001,6 +979,30 @@ void _glfwTerminateWayland(void) if (_glfw.wl.cursorTimerfd >= 0) close(_glfw.wl.cursorTimerfd); + // Free modules only after all wayland termination functions are called + 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; + } + _glfw_free(_glfw.wl.clipboardString); }