Windows should use _beginthreadex() instead of CreateThread(), to avoid a
authorRyan C. Gordon <icculus@icculus.org>
Wed, 04 Jan 2006 21:01:49 +0000
changeset 122509bc22169702
parent 1224 f033d9589ca1
child 1226 d05306f0dc21
Windows should use _beginthreadex() instead of CreateThread(), to avoid a
memory leak on each joined thread.
src/thread/win32/SDL_systhread.c
     1.1 --- a/src/thread/win32/SDL_systhread.c	Wed Jan 04 20:53:55 2006 +0000
     1.2 +++ b/src/thread/win32/SDL_systhread.c	Wed Jan 04 21:01:49 2006 +0000
     1.3 @@ -30,13 +30,14 @@
     1.4  #include <stdio.h>
     1.5  #include <stdlib.h>
     1.6  #include <windows.h>
     1.7 +#include <process.h>
     1.8  
     1.9  #include "SDL_error.h"
    1.10  #include "SDL_thread.h"
    1.11  #include "SDL_systhread.h"
    1.12  
    1.13  
    1.14 -static DWORD WINAPI RunThread(LPVOID data)
    1.15 +static unsigned __stdcall RunThread(void *data)
    1.16  {
    1.17  	SDL_RunThread(data);
    1.18  	return(0);
    1.19 @@ -44,9 +45,17 @@
    1.20  
    1.21  int SDL_SYS_CreateThread(SDL_Thread *thread, void *args)
    1.22  {
    1.23 -	DWORD  threadnum;
    1.24 +	unsigned threadid;
    1.25  
    1.26 -	thread->handle = CreateThread(NULL, 0, RunThread, args, 0, &threadnum);
    1.27 +	/*
    1.28 +	 * Avoid CreateThread: https://bugzilla.libsdl.org/show_bug.cgi?id=22
    1.29 +	 *
    1.30 +	 * have to use _beginthreadex if we want the returned handle
    1.31 +	 * to be accessible after the thread exits
    1.32 +	 * threads created with _beginthread auto-close the handle
    1.33 +	 */
    1.34 +	thread->handle = (SYS_ThreadHandle) _beginthreadex(NULL, 0, RunThread,
    1.35 +			args, 0, &threadid);
    1.36  	if (thread->handle == NULL) {
    1.37  		SDL_SetError("Not enough resources to create thread");
    1.38  		return(-1);