Fixed bug 1579 - Creating a texture with unsupported format may cause double-destruction
authorSam Lantinga
Fri, 28 Sep 2012 04:09:06 -0700
changeset 6497e844e2632149
parent 6496 d7cb947898d7
child 6498 93d9b2a2eb0e
child 6500 c532b3fdce27
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
     1.1 --- a/src/render/SDL_render.c	Fri Sep 28 04:03:06 2012 -0700
     1.2 +++ b/src/render/SDL_render.c	Fri Sep 28 04:09:06 2012 -0700
     1.3 @@ -393,6 +393,13 @@
     1.4              return NULL;
     1.5          }
     1.6  
     1.7 +        /* Swap textures to have texture before texture->native in the list */
     1.8 +        texture->native->next = texture->next;
     1.9 +        texture->prev = texture->native->prev;
    1.10 +        texture->native->prev = texture;
    1.11 +        texture->next = texture->native;
    1.12 +        renderer->textures = texture;
    1.13 +
    1.14          if (SDL_ISPIXELFORMAT_FOURCC(texture->format)) {
    1.15              texture->yuv = SDL_SW_CreateYUVTexture(format, w, h);
    1.16              if (!texture->yuv) {