Hi.

Is it nessesary to use FreeLibrary, if my program loaded a dll via LoadLibrary ?
I think, when my program ends, Windows will automaticaly use FreeLibrary and kick the dll out. I think, XP do so. Or not ?

Greetings,
Nordwind64
Posted on 2004-07-15 14:42:09 by Nordwind64
ofcourse it's not nessesary
Windows release all of memory when your program calls "ExitProcess".
Posted on 2004-07-15 14:53:44 by Criminal2
ExitProcess will signal the DLL and unload it.

ExitProcess is the preferred method of ending a process. This function provides a clean process shutdown. This includes calling the entry-point function of all attached dynamic-link libraries (DLLs) with a value indicating that the process is detaching from the DLL.
Posted on 2004-07-15 14:55:15 by donkey
It's not necessary, but it is "good practice" - if that means anything to you :)
Posted on 2004-07-15 16:46:45 by f0dder

It's not necessary, but it is "good practice" - if that means anything to you :)

In this case I am not sure whether it is good practice or bad, I don't think it makes any difference at all. Obviously the PELoader does not insert the FreeLibrary code for every DLL it loads, it lets the OS take care of the clean up, in the same way you can rely on DLL's loaded through LoadLibrary to do the same thing. It is simply redundant to call FreeLibrary, the ExitProcess API is very specific about the execution order:
After all attached DLLs have executed any process termination value, this function terminates the current process.
Posted on 2004-07-15 17:10:35 by donkey
The only case I can think of (right now anyway, quite knackered, been reading all day) where it might matter, is if the process is terminated abruptly (terminateprocess, or perhaps a crash) - I guess NT will do cleanup fine, but 9x might not. Of course this only matters if the DLL is used for a short period of time, if it's used for the lifetime of the app it wouldn't be cleaned anyway.

Yes, the above probably doesn't make much sense, I'm too trashed to express it any better atm :)
Posted on 2004-07-15 17:25:14 by f0dder
Thank you !
Posted on 2004-07-16 02:37:38 by Nordwind64