Skip to content

Commit

Permalink
Don't leak DirectSound thread handles
Browse files Browse the repository at this point in the history
The previous code refrained from calling CloseHandle on
_beingthreadex()-created threads, citing "documentation". Actually the
official docs suggest the exact opposite:

https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/endthread-endthreadex

> when you use _beginthreadex and _endthreadex, you must close the
> thread handle by calling the Win32 CloseHandle API

This means the previous code would leak the thread handle. This can be
confirmed empirically by looking at the process Win32 handle list in
e.g. Process Explorer.
  • Loading branch information
dechamps committed Jun 2, 2024
1 parent 18a606e commit 361a3b3
Showing 1 changed file with 1 addition and 7 deletions.
8 changes: 1 addition & 7 deletions src/hostapi/dsound/pa_win_ds.c
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,10 @@

#if !defined(__CYGWIN__) && !defined(UNDER_CE)
#define CREATE_THREAD (HANDLE)_beginthreadex
#undef CLOSE_THREAD_HANDLE /* as per documentation we don't call CloseHandle on a thread created with _beginthreadex */
#define PA_THREAD_FUNC static unsigned WINAPI
#define PA_THREAD_ID unsigned
#else
#define CREATE_THREAD CreateThread
#define CLOSE_THREAD_HANDLE CloseHandle
#define PA_THREAD_FUNC static DWORD WINAPI
#define PA_THREAD_ID DWORD
#endif
Expand Down Expand Up @@ -3051,9 +3049,7 @@ static PaError StartStream( PaStream *s )
#ifndef PA_WIN_DS_USE_WMME_TIMER
if( stream->processingThread )
{
#ifdef CLOSE_THREAD_HANDLE
CLOSE_THREAD_HANDLE( stream->processingThread ); /* Delete thread. */
#endif
CloseHandle( stream->processingThread ); /* Delete thread. */
stream->processingThread = NULL;
}
#endif
Expand Down Expand Up @@ -3092,10 +3088,8 @@ static PaError StopStream( PaStream *s )
if( WaitForSingleObject( stream->processingThreadCompleted, 30*100 ) == WAIT_TIMEOUT )
return paUnanticipatedHostError;

#ifdef CLOSE_THREAD_HANDLE
CloseHandle( stream->processingThread ); /* Delete thread. */
stream->processingThread = NULL;
#endif

}
#endif
Expand Down

0 comments on commit 361a3b3

Please sign in to comment.