CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    May 1999
    Location
    Fort Worth Texas
    Posts
    614

    Classes from a DLL



    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

  2. #2
    Join Date
    May 1999
    Location
    Oregon, USA
    Posts
    302

    Re: Classes from a DLL



    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



  3. #3
    Join Date
    May 1999
    Posts
    123

    Re: Classes from a DLL



    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.




Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured