Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- From: "Le Chaud Lapin" <jaibuduvin@xxxxxxxxx>
- Date: 22 Nov 2006 18:57:16 -0800
Skywing [MVP] wrote:
Note that a similar problem happens with thread creation, too. When a
thread is created, the first thing it does is call out to NTDLL, acquire the
LoaderLock, and make DllMain(DLL_THREAD_ATTACH) calls. As a result, you
can't block on a new thread in DllMain or you'll deadlock, just the same as
you can't block on waiting for a thread in the current process to exit
cleanly in DllMain.
Thanks for the thorough and clear explanation.
What is strange is that the hang happens only occasionally. The host
process is Explorer.EXE. The DLL is a shell namespace extension. I am
calling a two C++ member functions on a static global C++ object:
p.enregister() - DLL_PROCESS_ATTACH
p.deregister() - DLL_PROCESS_DETATCH
p.enregister() spawns a thread and goes about its business.
p.deregister() signals to that same thread to terminate, and waits for
it to terminate.
Because p is a global static object, the destructor of p will be
automatically invoked at program termination. The destructor of p, by
design, will call p.deregister() if the DLL is being unloaded
(absolutely necessary to call p.deregister() at some point before
unloading the DLL).
If I simple avoid doing a p.deregister() in DLL_PROCESS_DETACH, an
exception will be thrown, because, as it turns out, p.deregister()
depends on a global static mutex being in existence to work. That
mutex is not guaranteed to be remain in existence until all global
objects that need it have been destructed, per C++ translation unit
rules. You can rest assure that explicity controlling the order of
destruction of global objects by placing all global
constructible/destructible objects in one translation unit will ruin
the form of my code in ways I cannot describe.
So I figured the logical place to do enregister()/deregister() was in
DllMain.
But this still does not explain why hanging occurs only occasionally.
If it is true that the critical section is acquired before traversal of
the module list, then that would be true each time, so hanging should
occur every time. Just to be sure, I will run under the debugger to
make sure it works as I have it most times.
Any other ideas appreciated.
Regards,
-Le Chaud Lapin-
.
- Follow-Ups:
- Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- From: Pavel Lebedinsky [MSFT]
- Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- From: adebaene
- Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- From: Le Chaud Lapin
- Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- References:
- WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- From: Le Chaud Lapin
- Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- From: Skywing [MVP]
- WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- Prev by Date: Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- Next by Date: Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- Previous by thread: Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- Next by thread: Re: WaitForSingleObject On Thread Handle In DLL_PROCESS_DETACH
- Index(es):
Relevant Pages
|