From 3b5c59eca1113a8d9dcb3129559b22a392556c47 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Fri, 28 Sep 2012 04:09:06 -0700 Subject: [PATCH] Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction Alexander Hirsch 2012-08-25 20:01:29 PDT When creating a SDL_Texture with unsupported format (I'll now refer to it as texture A), SDL_CreateTexture will call SDL_CreateTexture again with GetClosestSupportedFormat to set texture->native (which I will now refer to as texture B). This causes texture B to be put before A in renderer->textures. If texture A is explicitly destroyed, everything is fine. Otherwise, upon SDL_DestroyRenderer, the loop will first encounter texture B, destroy it, then texture A, destroy that which will want to destroy texture->native and since it is already destroyed set an error. The solution could be as simple as swapping texture A with B after texture->native gets set in SDL_CreateTextures. --- src/render/SDL_render.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/render/SDL_render.c b/src/render/SDL_render.c index c810e967f..2c4777b60 100644 --- a/src/render/SDL_render.c +++ b/src/render/SDL_render.c @@ -393,6 +393,13 @@ SDL_CreateTexture(SDL_Renderer * renderer, Uint32 format, int access, int w, int return NULL; } + /* Swap textures to have texture before texture->native in the list */ + texture->native->next = texture->next; + texture->prev = texture->native->prev; + texture->native->prev = texture; + texture->next = texture->native; + renderer->textures = texture; + if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) { texture->yuv = SDL_SW_CreateYUVTexture(format, w, h); if (!texture->yuv) {