We are currently migrating Bugzilla to GitHub issues.
Any changes made to the bug tracker now will be lost, so please do not post new bugs or make changes to them.
When we're done, all bug URLs will redirect to their equivalent location on the new bug tracker.

Bug 3544 - Memory freeing bug in SDL_DestroyRenderer/SDL_DestroyTexture
Summary: Memory freeing bug in SDL_DestroyRenderer/SDL_DestroyTexture
Status: RESOLVED FIXED
Alias: None
Product: SDL
Classification: Unclassified
Component: render (show other bugs)
Version: HG 2.0
Hardware: x86_64 Windows 7
: P2 blocker
Assignee: Sam Lantinga
QA Contact: Sam Lantinga
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2017-01-04 22:14 UTC by felix
Modified: 2017-01-07 02:19 UTC (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description felix 2017-01-04 22:14:40 UTC
Here's a snippet of SDL_DestroyRenderer from hg revision 10746:95c57a177719:

    SDL_Texture *texture = NULL;
    SDL_Texture *nexttexture = NULL;
    /* ... */
    for (texture = renderer->textures; texture; texture = nexttexture) {
        nexttexture = texture->next;
        SDL_DestroyTexture(texture);
    }

SDL_DestroyTexture removes the texture from the linked list pointed to by the renderer and ends up calling SDL_DestroyTextureInternal, which contains this:

    if (texture->native) {
        SDL_DestroyTexture(texture->native);
    }

If it happens that texture->native is an alias of nexttexture two stack frames up, SDL_DestroyRenderer will end up trying to destroy an already freed texture. I've had this very situation happen in dosemu2.

Bug introduced in revision 10650:b6ec7005ca15, which has a somewhat ironic description of "Fixed all known static analysis bugs"...
Comment 1 Sam Lantinga 2017-01-06 08:32:26 UTC
Fixed, thanks!
https://hg.libsdl.org/SDL/rev/703e501cd0a6
Comment 2 Ryan C. Gordon 2017-01-06 08:43:13 UTC
(In reply to felix from comment #0)
> Bug introduced in revision 10650:b6ec7005ca15, which has a somewhat ironic
> description of "Fixed all known static analysis bugs"...

Sorry, that was my mistake; I didn't realize this called SDL_DestroyTexture internally, too.

--ryan.
Comment 3 Ryan C. Gordon 2017-01-07 02:19:28 UTC
The static analysis concern is more properly fixed by https://hg.libsdl.org/SDL/rev/d8a4f8a929b2  ... it thinks we're reusing a variable that we just freed because it doesn't appear to understand that this is var is getting changed elsewhere, so we add an assert to tell it that it's definitely a different value each time through the loop.

--ryan.