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 3317

Summary: Memory Leak in SDL_SetError when using std::thread
Product: SDL Reporter: Diego <diegoacevedo91>
Component: threadAssignee: Sam Lantinga <slouken>
Status: RESOLVED DUPLICATE QA Contact: Sam Lantinga <slouken>
Severity: normal    
Priority: P2 CC: icculus, sdlbugs
Version: HG 2.1   
Hardware: x86_64   
OS: Mac OS X (All)   
See Also: https://bugzilla.libsdl.org/show_bug.cgi?id=5041
Attachments: Leak producing code

Description Diego 2016-05-03 20:46:25 UTC
Created attachment 2438 [details]
Leak producing code

When SDL_SetError is called from a thread created using std::thread there are two memory leaks. One is caused by a malloc in SDL_GetErrBuf and the other is caused by a realloc in SDL_TLSSet.
There are no leaks when using SDL_Thread, only when using std::thread.

This is reported by using Instruments on my mac. I believe this is the compiler and related information:
Apple LLVM version 7.3.0 (clang-703.0.29)
Target: x86_64-apple-darwin15.4.0
Thread model: posix
Comment 1 Sam Lantinga 2017-08-12 03:44:10 UTC
This is because SDL_TLSCleanup() doesn't get called and SDL doesn't link the C++ runtime and has no way to automatically call it at thread termination.

In a future release we could export a thread cleanup function that could be called to take care of this.
Comment 2 sdlbugs 2020-03-05 08:19:59 UTC
SDL_Quit() doesn't call it either.
Comment 3 sdlbugs 2020-03-05 09:38:53 UTC
// (Sketch code of another repro case)

int main()
{
    SDL_SetMainReady();  // Can do normal startup, but this is simplest case
    SDL_Init(0);    
    SDL_Quit();

    return 0;
}   // Will leak memory allocated in SDL_ClearError()
Comment 4 sdlbugs 2020-03-05 09:46:53 UTC
exposing SDL_TLSCleanup() via an extern, instead of it being static, then calling it in SDL_Quit() clears up my variant of the problem.
Comment 5 sdlbugs 2020-03-17 08:58:32 UTC
Reported as separate bug because it has a separate repro case, but linked to this one. https://bugzilla.libsdl.org/show_bug.cgi?id=5041
Comment 6 Ryan C. Gordon 2020-03-25 00:24:40 UTC
Consolidating this with Bug #5041.

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