fix memory leak in wwme
[Trac: Originally reported by gordon_gidluck on 2008-08-28 12:56:54]
[Trac: Originally assigned to gordon_gidluck]
submitted by bodo.maass@sygyt.com on 08/22/2008
SUMMARYBRBR
All C Run-time functions except the signal() function work correctly when used in threads that are created by the CreateThread() function. However, depending on what CRT functions are called, there may be a small memory leak when threads are terminated. Calling strlen(), for example, does not trigger the allocation of the CRT thread data-block, and calling malloc(), fopen(), _open(), strtok(), ctime(), or localtime() causes allocation of a CRT per-thread data-block, which may cause a memory leak.
details are here:
http://support.microsoft.com/default.aspx/kb/104641
This defect also applies to wdmks and wasapi portAudio api's.
The solution is to use _beginthreadex() instead of CreateThread() and also _endthreadex() instead of ExitThread() .
[Trac: Originally assigned to gordon_gidluck]
submitted by bodo.maass@sygyt.com on 08/22/2008
SUMMARYBRBR
All C Run-time functions except the signal() function work correctly when used in threads that are created by the CreateThread() function. However, depending on what CRT functions are called, there may be a small memory leak when threads are terminated. Calling strlen(), for example, does not trigger the allocation of the CRT thread data-block, and calling malloc(), fopen(), _open(), strtok(), ctime(), or localtime() causes allocation of a CRT per-thread data-block, which may cause a memory leak.
details are here:
http://support.microsoft.com/default.aspx/kb/104641
This defect also applies to wdmks and wasapi portAudio api's.
The solution is to use _beginthreadex() instead of CreateThread() and also _endthreadex() instead of ExitThread() .
Leave a comment
[Trac: Originally commented by gordon_gidluck on 2008-08-28 12:56:54]
Changed CreateThread to _beginthreadex in pa_win_wdmks.c and pa_win_wmme.c . Changed ExitThread to _endthreadex. Ran into an issue with pa_win_wasapi.cpp . Will turn that over to David Viens to review before recommitting a change.
Changed CreateThread to _beginthreadex in pa_win_wdmks.c and pa_win_wmme.c . Changed ExitThread to _endthreadex. Ran into an issue with pa_win_wasapi.cpp . Will turn that over to David Viens to review before recommitting a change.
[Trac: Originally commented by gordon_gidluck on 2008-08-28 12:56:54]
cygwin compile reported broken by sb-lst@enotty.net
cygwin compile reported broken by sb-lst@enotty.net
[Trac: Originally commented by gordon_gidluck on 2008-08-28 12:56:54]
Changed the call to beginthreadex or CreateThread based upon whether or not __CYGWIN_ is defined.
{{{
/* use CreateThread for CYGWIN, beginthreadex for all others */
#ifndef __CYGWIN__
#define CREATE_THREAD (HANDLE)_beginthreadex( 0, 0, ProcessingThreadProc, stream, 0, &stream->processingThreadId )
#else
#define CREATE_THREAD CreateThread( 0, 0, ProcessingThreadProc, stream, 0, &stream->processingThreadId )
#endif
}}}
Since wasapi is C++, I employed typecasting to handle the more strict type-checking of parameters. I found a good tutorial related to this subject here:BR
http://www.mtholyoke.edu/courses/dstrahma/cs322/lab3andproj.htm
{{{
#ifndef __CYGWIN__
#define CREATE_THREAD (HANDLE) _beginthreadex(NULL, 0, (unsigned (_stdcall *)(void *))ProcThread, (LPVOID) stream, 0, (unsigned *)&stream->dwThreadId)
#else
#define CREATE_THREAD CreateThread(NULL, 0, ProcThread, (LPVOID) stream, 0, &stream->dwThreadId)
#endif
}}}
wdmks referenced _endthreadex so this was handled using the same conditional statement.
{{{
#ifndef __CYGWIN_
#define EXIT_THREAD _endthreadex(0)
#else
#define EXIT_THREAD ExitThread(0)
#endif
}}}
Changed the call to beginthreadex or CreateThread based upon whether or not __CYGWIN_ is defined.
{{{
/* use CreateThread for CYGWIN, beginthreadex for all others */
#ifndef __CYGWIN__
#define CREATE_THREAD (HANDLE)_beginthreadex( 0, 0, ProcessingThreadProc, stream, 0, &stream->processingThreadId )
#else
#define CREATE_THREAD CreateThread( 0, 0, ProcessingThreadProc, stream, 0, &stream->processingThreadId )
#endif
}}}
Since wasapi is C++, I employed typecasting to handle the more strict type-checking of parameters. I found a good tutorial related to this subject here:BR
http://www.mtholyoke.edu/courses/dstrahma/cs322/lab3andproj.htm
{{{
#ifndef __CYGWIN__
#define CREATE_THREAD (HANDLE) _beginthreadex(NULL, 0, (unsigned (_stdcall *)(void *))ProcThread, (LPVOID) stream, 0, (unsigned *)&stream->dwThreadId)
#else
#define CREATE_THREAD CreateThread(NULL, 0, ProcThread, (LPVOID) stream, 0, &stream->dwThreadId)
#endif
}}}
wdmks referenced _endthreadex so this was handled using the same conditional statement.
{{{
#ifndef __CYGWIN_
#define EXIT_THREAD _endthreadex(0)
#else
#define EXIT_THREAD ExitThread(0)
#endif
}}}