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 3673

Summary: SDL_FreeSurface is NOT thread safe
Product: SDL Reporter: Dan_ru <dan_ru>
Component: videoAssignee: Sam Lantinga <slouken>
Status: ASSIGNED --- QA Contact: Sam Lantinga <slouken>
Severity: blocker    
Priority: P2 CC: orangepikmin333
Version: 2.0.5   
Hardware: All   
OS: All   
Attachments: Source of test app

Description Dan_ru 2017-06-16 13:57:12 UTC
Created attachment 2763 [details]
Source of test app

Function SDL_FreeSurface() is NOT thread safe in all 2.0.X version of SDL.
Versions 1.X.X is free from bug.

Calling SDL_FreeSurface() on different surfaces can cause crashing ("double free").

As I see problem is in fuctions SDL_AllocFormat() and SDL_FreeFormat() - currently work with global list "static SDL_PixelFormat *formats;" is not under mutex.


Also I can suggest to move calling SDL_FreeFormat() from SDL_FreeSurface() to SDL_QuitSubSystem(SDL_INIT_VIDEO) and/or SDL_Quit().

Source of test app is attched.
Comment 1 orangepikmin333 2020-08-24 18:40:05 UTC
I ran into this issue yesterday after refactoring my surface loading logic and, after several hours of frustration, I decided that the problem probably lies in SDL itself. My surface is stored in a wrapper that calls SDL_FreeSurface, and that wrapper is stored in a smart pointer - as such, it is impossible for my program to have freed the same surface twice. Furthermore, this issue doesn't occur when I comment out SDL_FreeSurface, so I'm certain that the problem has to do with it.

Has anything been done for this issue? I'm thinking I might have to just single-thread my surface loading for now.

Additional info: while this issue occurred for me on Linux, I could not reproduce it on Windows.