Re: DllMain is called when....
From: Igor Tandetnik (itandetnik_at_mvps.org)
Date: 07/29/04
- Next message: Tobias Güntner: "Re: pure virtual destructor"
- Previous message: Charles R: "Cant insert breakpoints."
- In reply to: Roger: "DllMain is called when...."
- Next in thread: Roger: "Re: DllMain is called when...."
- Reply: Roger: "Re: DllMain is called when...."
- Messages sorted by: [ date ] [ thread ]
Date: Thu, 29 Jul 2004 16:21:40 -0400
"Roger" <Roger@discussions.microsoft.com> wrote in message
news:FEDE7415-C932-42CB-8EDE-10E74E5CBD4F@microsoft.com
> 1. called with DLL_PROCESS_ATTACH
> 2. called with DLL_THREAD_ATTACH
> 3. called with DLL_THREAD_ATTACH
> 4. called with DLL_THREAD_DETACH
> 5. called with DLL_PROCESS_DETACH
>
> Most disconcerting is that the last DLL_PROCESS_DETACH is called
> before I call FreeLibrary.(!) Can anyone tell me:
>
> 1. Why there are calls with DLL_THREAD_ATTACH when I am not calling
> LoadLibrary from more than one thread, and additionally I'm not
> accessing the dll from more than the one object in which is it
> created
A newly created thread calls DllMain(DLL_THREAD_ATTACH) for every DLL
currently loaded into the process. This is completely unrelated to
whether this thread ever calls LoadLibrary, or ever referes to anything
from a particular DLL.
> 2. Why there are "unbalanced" calls with DLL_THREAD_ATTACH and
> DLL_THREAD_DETACH
OS makes no attempts to balance them. When a DLL is loaded, there may be
threads running already - they don't call DLL_THREAD_ATTACH. Only a new
thread started after the DLL is loaded calls DLL_THREAD_ATTACH.
By the same token, a thread terminating gracefully calls
DLL_THREAD_DETACH on all DLLs currently loaded into the process right
before terminating. When a DLL is unloaded, only DLL_PROCESS_DETACH is
called - you don't get DLL_THREAD_DETACH for every thread currently
running.
Thus, the following situations are possible:
a. A thread existed before the DLL was loaded, and exited while it was
loaded. Such a thread will execute DLL_PROCESS_DETACH but not
DLL_PROCESS_ATTACH
b. A thread was created while the DLL was loaded, then the DLL is
unloaded while this thread is still running. In this case you will have
DLL_PROCESS_ATTACH for this thread, but not DLL_PROCESS_DETACH.
> 3. Why the call with DLL_PROCESS_DETACH takes place before I call
> FreeLibrary
Are you sure? I find it hard to believe. Is there a possibility of a
reference counting error (FreeLibrary called more times than LoadLibrary
on the same DLL)?
--
With best wishes,
Igor Tandetnik
"For every complex problem, there is a solution that is simple, neat,
and wrong." H.L. Mencken
- Next message: Tobias Güntner: "Re: pure virtual destructor"
- Previous message: Charles R: "Cant insert breakpoints."
- In reply to: Roger: "DllMain is called when...."
- Next in thread: Roger: "Re: DllMain is called when...."
- Reply: Roger: "Re: DllMain is called when...."
- Messages sorted by: [ date ] [ thread ]
Relevant Pages
|