SDL_CreateThread crash on return


I did a LuaJIT binding for SDL 2.0.10 in GitHub - sonoro1234/LuaJIT-SDL2: autogenerated LuaJIT bindings for SDL2 with threads and audio
I have always compiled with mingw-w64 on windows 7 without MSYS
A simple program calling SDL_CreateThread had always worked:
LuaJIT-SDL2/main.lua at master · sonoro1234/LuaJIT-SDL2 · GitHub
When I updated SDL to 2.0.12 I got a SIGSEGV after the thread has finished.

Thread 3 received signal SIGSEGV, Segmentation fault.
[Switching to Thread 5396.0x1a0c]
0x0fa70ff7 in ?? ()
(gdb) thread apply all bt

Thread 3 (Thread 5396.0x1a0c):
#0  0x0fa70ff7 in ?? ()
#1  0x6769008d in RunThread (data=0x356058) at C:\LuaGL\gitsources\anima\LuaJIT-SDL2\SDL\src\thread\windows\SDL_systhread.c:92
#2  0x676900a5 in RunThreadViaCreateThread (data=0x356058) at C:\LuaGL\gitsources\anima\LuaJIT-SDL2\SDL\src\thread\windows\SDL_systhread.c:99
#3  0x76d4343d in KERNEL32!BaseThreadInitThunk () from C:\Windows\syswow64\kernel32.dll
#4  0x77539812 in ntdll!RtlInitializeExceptionChain () from C:\Windows\SysWOW64\ntdll.dll
#5  0x775397e5 in ntdll!RtlInitializeExceptionChain () from C:\Windows\SysWOW64\ntdll.dll
#6  0x00000000 in ?? ()

Also found with dependency walker: SDL 2.0.10 takes _beginthreadex from MSVCRT.dll but SDL 2.0.12 does not.

I could saw some changes in SDL threads code from 2.0.10 to 2.0.12 but I was not able to find what should be changed. Does anyone have some clue?

Victor Bombi

If I change SDL_CreateThread definition from

SDL_Thread * SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data);


SDL_Thread * SDL_CreateThread(SDL_ThreadFunction fn, const char *name, void *data,pfnSDL_CurrentBeginThread bf,pfnSDL_CurrentEndThread ef);

and use the second providing _beginthreadex and _endthreadex the SIGSEV is gone.

But I still dont understand what has changed in SDL from 2.0.10 to 2.0.12.
( I would prefer the 2.0.10 behaviour as the LuaJIT binding was the same on windows and linux)