From aed68e9e6ee929159760ccd85ed1464ca04587e3 Mon Sep 17 00:00:00 2001 From: Patryk Obara Date: Tue, 17 Mar 2020 22:54:02 +0100 Subject: [PATCH] Destroy SDL resources in the proper order Prevent internal SDL2 asserts on Windows 10 when using output=texture, also improves stability on AmigaOS and Linux using Wayland. --- src/gui/sdlmain.cpp | 64 +++++++++++++++++++++++++++------------------ 1 file changed, 38 insertions(+), 26 deletions(-) diff --git a/src/gui/sdlmain.cpp b/src/gui/sdlmain.cpp index 929e700a..61a5b696 100644 --- a/src/gui/sdlmain.cpp +++ b/src/gui/sdlmain.cpp @@ -293,10 +293,9 @@ struct SDL_Block { const char *rendererDriver; int displayNumber; struct { - SDL_Texture *texture; - SDL_PixelFormat *pixelFormat; + SDL_Texture *texture = nullptr; + SDL_PixelFormat *pixelFormat = nullptr; } texture; - SDL_cond *cond; struct { int xsensitivity; int ysensitivity; @@ -319,6 +318,8 @@ struct SDL_Block { static SDL_Block sdl; +static void CleanupSDLResources(); + #if C_OPENGL static char const shader_src_default[] = "varying vec2 v_texCoord;\n" @@ -520,24 +521,9 @@ static int int_log2 (int val) { static SDL_Window * GFX_SetSDLWindowMode(Bit16u width, Bit16u height, bool fullscreen, SCREEN_TYPES screenType) { static SCREEN_TYPES lastType = SCREEN_SURFACE; - if (sdl.renderer) { - SDL_DestroyRenderer(sdl.renderer); - sdl.renderer = 0; - } - if (sdl.texture.pixelFormat) { - SDL_FreeFormat(sdl.texture.pixelFormat); - sdl.texture.pixelFormat = 0; - } - if (sdl.texture.texture) { - SDL_DestroyTexture(sdl.texture.texture); - sdl.texture.texture = 0; - } -#if C_OPENGL - if (sdl.opengl.context) { - SDL_GL_DeleteContext(sdl.opengl.context); - sdl.opengl.context = 0; - } -#endif + + CleanupSDLResources(); + int currWidth, currHeight; if (sdl.window && sdl.resizing_window) { SDL_GetWindowSize(sdl.window, &currWidth, &currHeight); @@ -1596,13 +1582,39 @@ void GFX_UpdateDisplayDimensions(int width, int height) } } -static void GUI_ShutDown(Section * /*sec*/) { - GFX_Stop(); - if (sdl.draw.callback) (sdl.draw.callback)( GFX_CallBackStop ); - if (sdl.desktop.fullscreen) GFX_SwitchFullScreen(); - if (mouse_is_captured) GFX_ToggleMouseCapture(); +static void CleanupSDLResources() +{ + if (sdl.texture.pixelFormat) { + SDL_FreeFormat(sdl.texture.pixelFormat); + sdl.texture.pixelFormat = nullptr; + } + if (sdl.texture.texture) { + SDL_DestroyTexture(sdl.texture.texture); + sdl.texture.texture = nullptr; + } + if (sdl.renderer) { + SDL_DestroyRenderer(sdl.renderer); + sdl.renderer = nullptr; + } +#if C_OPENGL + if (sdl.opengl.context) { + SDL_GL_DeleteContext(sdl.opengl.context); + sdl.opengl.context = 0; + } +#endif } +static void GUI_ShutDown(Section *) +{ + GFX_Stop(); + if (sdl.draw.callback) + (sdl.draw.callback)( GFX_CallBackStop ); + if (sdl.desktop.fullscreen) + GFX_SwitchFullScreen(); + if (mouse_is_captured) + GFX_ToggleMouseCapture(); + CleanupSDLResources(); +} static void SetPriority(PRIORITY_LEVELS level) {