|
-
July 27th, 2010, 06:39 AM
#1
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!
-
July 27th, 2010, 06:47 AM
#2
Re: LoadLibrary Question!
Free it right after you use the extracted functions
Not to free it equals to a bad behavior to causing memory leaks
hi,,,
-
July 27th, 2010, 08:07 AM
#3
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
-
July 27th, 2010, 08:12 PM
#4
Re: LoadLibrary Question!
 Originally Posted by Igor Vartanov
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|