Tuesday, November 07, 2006

"LoaderLock was detected" Error when debugging my .NET DirectX code sample:

When tried to develop a video player in .NET 2.0 and Visual Studio 2005 Team Suite using DirectX SDK I was stuck with this error. I tried the same code in .NET1.1 and Visual Studio 2003, the code runs fine, no "LoaderLock was detected" error. Then I started exploring about the error I got while using Visual Studio 2005.

Check this original error message:

LoaderLock was detected
DLL 'C:\WINDOWS\assembly\GAC\Microsoft.DirectX\1.0.2902.0__31bf3856ad364e35\Microsoft.DirectX.dll' is attempting managed execution inside OS Loader lock. Do not attempt to run managed code inside a DllMain or image initialization function since doing so can cause the application to hang.

Managed Debugging Assistants is also referred as MDA, MDA work with the Common Language Runtime (CLR) to provide information on runtime state. They generate information about runtime events. Load Lock is one of the MDA available in .NET framwork, the code part which exist in all Managed DirectX 1.1 assemblies, is the reason to MDA firing. The most common failure when executing code inside the operating system's loader lock is that threads will deadlock when attempting to call other Win32 functions that also require the loader lock.

The only thing that seems to hold this situation is simply to turn off the MDA for LoaderLock, which is not recommended solution.

Global registry key which is used to turn on/off MDA.

[HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\.NETFramework]
"MDA"="1"

Better options to work around this problem are...

Look at the stack trace for the thread that has activated this MDA. The thread is attempting to illegally call into managed code while holding the operating system's loader lock. You will probably see a DLL's DllMain or equivalent entry point on the stack. The operating system's rules for what you can legally do from inside such an entry point are quite limited. These rules preclude any managed execution.

To go back to Visual Studio 2003 and .NET 1.1 . Else disable the loader lock Managed Debugging Assistants. Debug/Exceptions, Open the Managed Debugging Assistants tree node and uncheck Loader Lock. This setting is per solution so it will only affect this solution.

Tips:

In VS2005, go to the Debug->Exceptions menu. Within the Exceptions dialog expand the "Managed Debugging Assistants" node. Look for LoaderLock and uncheck the "Thrown" column.
Note: Do not disable all MDAs by setting MDA="0" in the registry as it causes problems.

Mixed managed/unmanaged C++ assemblies built for .NET Framework versions 1.0 or 1.1 generally attempt to execute managed code inside the loader lock unless special care has been taken. .NET 1.1 Managed DirectX assemblies when used with Visual Studio 2005 and .Net 2.0.

Learn About MDA:

Diagnosing Errors with Managed Debugging Assistants

Microsoft Helps related to this problem:

Managed Debug Assistants (MDAs) are cool.

Microsoft Visual Studio 2005 to debug a Microsoft Foundation Classes (MFC) class library application

Mixed DLL Loading Problem

9 comments:

Elmer said...

Hi,

I found iTunes was the problem in my case. Killing iTunes made the error message go away.

Cheers! Elmer
(programming.torensma.net)

Unknown said...

Very helpful. Thanks!

Shalvin said...

Thank you for blogging on LoaderLock. I encountered the error while runninin the Alchemi Grid Computing project version 0.6.1 in VS 2005.

Antony Leo Edel said...

Thank you so much

Unknown said...

Thank you ... It was really Helpful !!!

3AnhEm said...

Thank you :)

3AnhEm said...

Thank you :)

Gabriel said...

You just saved me! Thank you!

Unknown said...

hi
while saving the audio record. u had give a code that computer c= new computer.
i am getting an error .

ASP.NET MVC - Sport Facility Booking system

  The project relies on some external service providers. Thus accessing them via their API requires authentication. An API Key need to be su...