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 4055

Summary: Memory leak on quit because SDL_TLSCleanup isn't called on the main thread
Product: SDL Reporter: Jack Powell <jack9267>
Component: threadAssignee: Sam Lantinga <slouken>
Status: RESOLVED DUPLICATE QA Contact: Sam Lantinga <slouken>
Severity: major    
Priority: P2 CC: icculus, sezeroz
Version: 2.0.7   
Hardware: x86_64   
OS: Windows 10   
See Also: https://bugzilla.libsdl.org/show_bug.cgi?id=5041

Description Jack Powell 2018-01-19 17:46:34 UTC
SDL leaks memory on quit of the application. This can be found out by using Microsoft's built in CRT leak detection.

I fixed the leaks in my build of SDL by simply adding a call to SDL_TLSCleanup in SDL_Quit. The issue is thread-local storage is not cleaned up for the main thread. Because of my complex requirements I have made SDL_TLSCleanup a public function which would be something I think would be good for advanced users.

The leak you'll notice in SDL is the error string since it is TLS! The memory won't get freed until SDL_TLSCleanup is called so adding the call in SDL_Quit seems like a perfect solution for main thread quitting although maybe just incase the function should be exported and the winmain/main wrapper should call SDL_TLSCleanup (the one that calls SDL_SetMainReady)?

I've flagged this as major since the TLS not being cleaned up can cause issues where important cleanup is not done such as flushing settings or open data handles. This could cause loss or corruption of data in some applications.

Bug "3317 Memory Leak in SDL_SetError when using std::thread" may also relate to this. Adding SDL_TLSCleanup as an exported/public function would resolve third party threading such as std::thread as one could simply call SDL_TLSCleanup once the thread quits. Although applications using SDL should use SDL's create thread in my opinion.

Many Thanks
Jack Powell
Comment 1 Ryan C. Gordon 2020-03-25 00:25:06 UTC
Consolidating this with Bug #5041.

*** This bug has been marked as a duplicate of bug 5041 ***