Dll Cleanup Bug
-
The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following:
hThread
is a running threadhTerminate
is an event that signals the thread to terminateBOOL APIENTRY DllMain(
HMODULE,
DWORD reasonForCall,
LPVOID)
{
if (reasonForCall == DLL_PROCESS_DETACH)
SetEvent(hTerminate);
if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
TerminateThread(hThread, 0);
CloseHandle(hThread);
CloseHandle(hTerminate);
break;
}
return TRUE;
}Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following:
hThread
is a running threadhTerminate
is an event that signals the thread to terminateBOOL APIENTRY DllMain(
HMODULE,
DWORD reasonForCall,
LPVOID)
{
if (reasonForCall == DLL_PROCESS_DETACH)
SetEvent(hTerminate);
if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
TerminateThread(hThread, 0);
CloseHandle(hThread);
CloseHandle(hTerminate);
break;
}
return TRUE;
}Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
Doing anything like that in
DllMain()
is a Bad Thing. TheWaitForSingleObject()
will always timeout because it is being called while the loader lock is held, which prevents the other thread from exiting.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
-
Doing anything like that in
DllMain()
is a Bad Thing. TheWaitForSingleObject()
will always timeout because it is being called while the loader lock is held, which prevents the other thread from exiting.--Mike-- Visual C++ MVP :cool: LINKS~! Ericahist | PimpFish | CP SearchBar v3.0 | C++ Forum FAQ
I posted this because I've seen this many times. Many developers don't notice because the leak or timeout happens when the entire process is exiting. I consider it one of the major design bugs in Win32.
Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
-
The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following:
hThread
is a running threadhTerminate
is an event that signals the thread to terminateBOOL APIENTRY DllMain(
HMODULE,
DWORD reasonForCall,
LPVOID)
{
if (reasonForCall == DLL_PROCESS_DETACH)
SetEvent(hTerminate);
if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
TerminateThread(hThread, 0);
CloseHandle(hThread);
CloseHandle(hTerminate);
break;
}
return TRUE;
}Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke
I'd guess it's a loader lock[^] problem.
Steve
-
The following code is from a DLL. The thread in question had allocated resources. It has been simplified but assume the following:
hThread
is a running threadhTerminate
is an event that signals the thread to terminateBOOL APIENTRY DllMain(
HMODULE,
DWORD reasonForCall,
LPVOID)
{
if (reasonForCall == DLL_PROCESS_DETACH)
SetEvent(hTerminate);
if (WaitForSingleObject(hThread, 5000) == WAIT_TIMEOUT)
TerminateThread(hThread, 0);
CloseHandle(hThread);
CloseHandle(hTerminate);
break;
}
return TRUE;
}Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke