Welcome to MSDN Blogs Sign in | Join | Help

The NT DLL Loader: FreeLibrary()

Let's review the loader's modus operandi and derive the (once again simple!) rules for what the heck is going on.

When someone calls FreeLibrary(hInstance), the loader walks the closure of the dependencies for the module/instance in question and if their refcounts are not maxxed out ("pinned"), they are decremented.  Any modules whose refcounts are now zero are marked for unload.  This means that the loader finds all of those modules in the in-initialization-order list and runs their DLL_PROCESS_DETACH callouts in the opposite order of initialization.  Notice that the return value from DllMain() is ignored for DLL_PROCESS_DETACH - you cannot do anything which could fail.  After shutting down all those DLLs, they are unmapped and FreeLibrary() returns.

Notice that the ordering dragon reared its head again but also notice that as long as your init order made sense, it's very likely that your uninit order makes sense also so you tend not to hit so many problems here w.r.t. ordering.  Most LoadLibrary()/FreeLibrary() ordering issues are found in the LoadLibrary() path, not the unload path.  (In fact I can't recall a single instance of FreeLibrary() inducing a DLL_PROCESS_DETACH [basic] ordering issue.  Not that you can't imagine it but I can't recall ever having such a problem and as you know I like to talk about problems.)

Reentrancy problems tomorrow...  Just imagine, given this information, what happens if someone calls LoadLibrary() during DLL_PROCESS_DETACH...

Published Tuesday, June 28, 2005 10:39 PM by mgrier

Comments

# re: The NT DLL Loader: FreeLibrary()

Wednesday, June 29, 2005 3:57 PM by Steven Edwards
Ok so explain to me this. The foo.exe.local override support for loading local dlls first. The behavior of LoadLibrary and friends and the search order has changed so many times that I wonder does the *.exe.local trick even matter anymore?

# re: The NT DLL Loader: FreeLibrary()

Friday, July 01, 2005 12:04 AM by an0nym0us
> Most LoadLibrary()/FreeLibrary() ordering issues are found in the LoadLibrary() path, not the unload path.

There seem to be ordering issues during process termination when a dll is loaded dynamically.

# re: The NT DLL Loader: FreeLibrary()

Thursday, July 21, 2005 5:22 AM by Wound
I suspect I'm looking at a related problem right now. We have a COM exe server, which has created any windows script component, and then, in exit(0) called from WinMainCRTStartup we get an access violation in scrobj.dll. The weird thing is that it only crashes on a couple of old PC's and the others just get a first chance exception.

Does this sound like the kind of thing you'd expect to see in a FreeLibrary issue?

# re: The NT DLL Loader: FreeLibrary()

Monday, August 01, 2005 11:18 PM by No comment
> Reentrancy problems tomorrow...

If you need help making time go faster, see Larry Osterman's blog from last week.

# What every programmer should know about DLL loading

Sunday, October 08, 2006 5:59 AM by Cluebat-man to the rescue

Every programmer who ventures beyond his / her first hello world application has to deal with DLLs sooner

# How DLL exports really work

Friday, September 12, 2008 6:51 AM by Cluebat-man to the rescue

I found this list of article on Raymond's blog . Raymond's blog is one of the more interesting

New Comments to this post are disabled
 
Page view tracker