CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jan 2008
    Posts
    98

    LoadLibrary Question!

    I have a dll to export some functions, first use LoadLibrary to load the dll,
    Then use GetProcAddress to get the export functions.
    My question is: When to use FreeLibrary? after the use of all the export functions?
    If I FreeLibrary already, then I can't use the export functions, right?
    And what's the side effect if I don't call FreeLibrary, will it lead memory leak?

    Thanks for being patient to answer my questions!
    I thought the code would do the right thing as I expected, but nothing happens, it's really awful!

  2. #2
    Join Date
    Aug 2008
    Posts
    112

    Re: LoadLibrary Question!

    When to use FreeLibrary?
    Free it right after you use the extracted functions
    Not to free it equals to a bad behavior to causing memory leaks
    hi,,,

  3. #3
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,633

    Re: LoadLibrary Question!

    Loading library implies the dll code and data sections are mapped to your process address space, all import table entries resolved, global variables initialized and DllMain called.

    Freeing library means the dll code and data sections appear unmapped. This means, in case you try to call previously linked export from the dll, you're gonna have access violation exception because of dereferencing address nonexistent to the moment. So, you need to free library only when you're sure all the dll functions are to be never called anymore, explicitly or implicitly.

    Non-freeing library surely results in wasting address space, but I hardly can imagine how it may result in memory leak. 'Memory leak' situation is typically about losing a reference to memory fragment in a heap (of any kind, including CRT heap), and non-freed library has nothing common with a heap. And it hardly can be about handle leak, as long as loaded library handle can be quite easily restored by GetModuleHandle call.
    Best regards,
    Igor

  4. #4
    Join Date
    Jan 2008
    Posts
    98

    Re: LoadLibrary Question!

    Quote Originally Posted by Igor Vartanov View Post
    Loading library implies the dll code and data sections are mapped to your process address space, all import table entries resolved, global variables initialized and DllMain called.

    Freeing library means the dll code and data sections appear unmapped. This means, in case you try to call previously linked export from the dll, you're gonna have access violation exception because of dereferencing address nonexistent to the moment. So, you need to free library only when you're sure all the dll functions are to be never called anymore, explicitly or implicitly.

    Non-freeing library surely results in wasting address space, but I hardly can imagine how it may result in memory leak. 'Memory leak' situation is typically about losing a reference to memory fragment in a heap (of any kind, including CRT heap), and non-freed library has nothing common with a heap. And it hardly can be about handle leak, as long as loaded library handle can be quite easily restored by GetModuleHandle call.
    Your detailed explanation really unlocked my mind. Thank you very much!
    I thought the code would do the right thing as I expected, but nothing happens, it's really awful!

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