[Interest] calling QMessageBox::warning() may break the DLL detaching on VS2010

Sascha Cunz sascha-ml at babbelbox.org
Thu Nov 15 13:15:57 CET 2012


> I'm amazed that window related calls like ::MessageBox are even allowed in
> DllMain, I certainly wouldn't expect them to work!

Actually, they aren't - and that for a good reason. If it works, it's plainly 
by accident.

Rationale: Creating a HWND in Windows might trigger the loading of DLLs. 
However, a non-recursive mutex (it's commonly called "LoaderMutex") is held by 
the system during processing of DllMain. Because of that, calls to DllMain are 
serialised in a queue, which results in: "If you load a dll from within 
DllMain, you can be sure it is not yet initialized".

I remember at least those 2 scenarios, where I've had much trouble with this:
- Mixing managed and unmanaged code in one application.
- Delayed-Loading of DLLs.

>From http://msdn.microsoft.com/en-
us/library/windows/desktop/ms682583%28v=vs.85%29.aspx

> Calling functions that require DLLs other than Kernel32.dll may result in 
problems that are difficult to diagnose. For example, calling User, Shell, and 
COM functions can cause access violation errors, because some functions load 
other system components. Conversely, calling functions such as these during 
termination can cause access violation errors because the corresponding 
component may already have been unloaded or uninitialized.

> Because DLL notifications are serialized, entry-point functions should not 
attempt to communicate with other threads or processes. Deadlocks may occur as 
a result.

Sascha



More information about the Interest mailing list