Jim Bassett
March 29th, 1999, 11:28 AM
I am building my DLLs using __declspec(dllinport/dllexport) so I can use classes in my DLL. So my question is:
If I have a DLL that has two or more classes in it and I declare an instance of any one class, is the whole DLL being loaded? Also if I create instances of more than one class in different functions of my client and lose scope in one of the functions, does the whole DLL get unloaded?
Thanks
Jim Bassett
Gomez Addams
March 29th, 1999, 11:56 AM
1. Yes, the entire DLL is loaded for any one instance.
2. No, the DLL will not be unloaded.
If you implement a DllMain function for your DLL you will see
how some of this happens. There are four reasons DllMain will
be called - thread attach, thread detach, process attach, and
process detach. Put a TRACE statement in there for each of
them and you will see the load and unload sequence. Unloading
happens for the process detach reason.
In MFC, this happens in DllInit.cpp
Jerry Coffin
March 29th, 1999, 12:25 PM
When you use part of a DLL, the entire DLL image gets mapped to memory locations. Then, when you access particular memory locations, they will be paged in from the DLL. Parts that you never access, never get loaded. This is done on a page-by-page basis, meaning loading is done in 4 kilobyte chunks. Therefore, it's best to keep parts of the program that are used together also located near each other in the DLL.
If/when the DLL hasn't been used recently, something else may be loaded into that memory instead. When/if no process is loaded that contains a reference to the DLL, then it is unmapped. However, even then, the system only marks it as eligible to be overwritten rather than unloading it immediately -- this way, if the code doesn't get used again soon, something else can make use of the memory. However, if you immediately load another program that uses the same DLL, the memory manager "remembers" that it still has the DLL mapped, and doesn't bother with mapping it over again.
I'm basing the details here on NT -- Windows 95/98 may be somewhat different in a few respects, but most of the general idea should be similar.