SDL: video: Unload GL/Vulkan when shutting down the video subsystem

From 37fca1fdcf75dd62b60b364ab747b17e661d85fc Mon Sep 17 00:00:00 2001
From: Frank Praznik <[EMAIL REDACTED]>
Date: Sun, 21 Dec 2025 11:11:44 -0500
Subject: [PATCH] video: Unload GL/Vulkan when shutting down the video
 subsystem

If GL/Vulkan was loaded manually via a SDL_X_LoadLibrary() call instead of via window flags or through the GPU or renderer system, and there is no matching SDL_X_UnloadLibrary() call, the library instance won't be automatically unloaded while shutting down the video system, as the refcount won't go to zero, which can cause problems with some backends, and leaves a leaked DLL/SO handle when the global video object is destroyed.

Ensure that the libraries are unloaded after destroying the windows, but before shutting down the video backend, to prevent a leak and possible driver errors when shutting down the video backend.
---
 src/video/SDL_video.c | 10 ++++++++++
 1 file changed, 10 insertions(+)

diff --git a/src/video/SDL_video.c b/src/video/SDL_video.c
index 4aea9f6a77dea..f1c25d11cf31b 100644
--- a/src/video/SDL_video.c
+++ b/src/video/SDL_video.c
@@ -4667,6 +4667,16 @@ void SDL_VideoQuit(void)
     while (_this->windows) {
         SDL_DestroyWindow(_this->windows);
     }
+
+    if (_this->gl_config.driver_loaded && _this->GL_UnloadLibrary) {
+        _this->GL_UnloadLibrary(_this);
+        _this->gl_config.driver_loaded = 0;
+    }
+    if (_this->vulkan_config.loader_loaded && _this->Vulkan_UnloadLibrary) {
+        _this->Vulkan_UnloadLibrary(_this);
+        _this->vulkan_config.loader_loaded = 0;
+    }
+
     _this->VideoQuit(_this);
 
     for (i = _this->num_displays; i--; ) {