From fcf006780e5c55d88192d53b72019ae6a8260315 Mon Sep 17 00:00:00 2001 From: "Ryan C. Gordon" Date: Wed, 4 Jan 2006 21:01:49 +0000 Subject: [PATCH] Windows should use _beginthreadex() instead of CreateThread(), to avoid a memory leak on each joined thread. --- src/thread/win32/SDL_systhread.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/thread/win32/SDL_systhread.c b/src/thread/win32/SDL_systhread.c index fb38cd139..b705344c4 100644 --- a/src/thread/win32/SDL_systhread.c +++ b/src/thread/win32/SDL_systhread.c @@ -30,13 +30,14 @@ static char rcsid = #include #include #include +#include #include "SDL_error.h" #include "SDL_thread.h" #include "SDL_systhread.h" -static DWORD WINAPI RunThread(LPVOID data) +static unsigned __stdcall RunThread(void *data) { SDL_RunThread(data); return(0); @@ -44,9 +45,17 @@ static DWORD WINAPI RunThread(LPVOID data) int SDL_SYS_CreateThread(SDL_Thread *thread, void *args) { - DWORD threadnum; - - thread->handle = CreateThread(NULL, 0, RunThread, args, 0, &threadnum); + unsigned threadid; + + /* + * Avoid CreateThread: https://bugzilla.libsdl.org/show_bug.cgi?id=22 + * + * have to use _beginthreadex if we want the returned handle + * to be accessible after the thread exits + * threads created with _beginthread auto-close the handle + */ + thread->handle = (SYS_ThreadHandle) _beginthreadex(NULL, 0, RunThread, + args, 0, &threadid); if (thread->handle == NULL) { SDL_SetError("Not enough resources to create thread"); return(-1);