105

I am building on C++ dll, by writing code in C#.

I get an error, saying

LoaderLock was detected Message: 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.

I tried seraching what this error exactly means, but I am drawing pointless articles, mostly saying that it's just a warning, and I should switch that off in Visual Studio. The other solutions seem to be due to ITunes, or this problem occurring when programming with DirectX. My problem is connected to neither.

Can anybody explain, what this actually means?

3
  • I feel with you, I got the same problem, and what surprises me most: my dll is not even managed code, so why/how is it supposed to use managed code on the (non-existent) DllMain??
    – Sam
    Mar 5, 2010 at 14:04
  • I got this warning while attempting to view the contents of a dataset in debug mode. I am using c#, it happened in a regular windows form.
    – Soenhay
    May 19, 2015 at 14:55
  • Since you can't figure out the cause (as you commented in the top answer) I suspect there is a dll you are loading that is committing the crime. Jan 30, 2019 at 0:12

9 Answers 9

77

you need to go to menu Debug -> Exceptions, open the Managed Debugging Assistants, find LoaderLock and uncheck

http://goo.gl/TGAHV

6
  • 23
    yes, this is the way to turn off the warning; But even after 2 years I haven't figured out exactly why it was happening. Nov 4, 2010 at 12:41
  • 2
    This happened to me opening an old project in VS 2012
    – 4imble
    Aug 7, 2012 at 9:59
  • 1
    I'm with you @Kohan I have also opened an older project and got the error. I have disabled the exception but would like to understand what can be done to prevent this.
    – Pimenta
    Feb 14, 2013 at 17:44
  • 1
    If I run the project as Native debugging, with all exceptions on default (reset all), the debug window shows <mda:msg xmlns:mda="schemas.microsoft.com/CLR/2004/10/mda"> <!-- Attempting managed execution inside OS Loader lock.... etc --> <mda:loaderLockMsg break="true"/> </mda:msg> VS then presents multiple breakpoints during the CTOR sequence. Turning off the LoaderLock setting doesn't help. For me, I had to tick the top MDA option (for ALL MDA) then untick the top level option (for no MDA), then build+run. This didn't work for my colleague. Oct 15, 2014 at 12:29
  • 19
    Wanted to share an update in VS2015, you now need to go to Debug->Windows->Exception Settings. The rest is the same with Managed Debugging Assistants \ LoaderLock
    – jxramos
    Aug 3, 2016 at 19:10
55

The general idea of loader lock: The system runs the code in DllMain inside a lock (as in - synchronization lock). Therefore, running non-trivial code inside DllMain is "asking for a deadlock", as described here.

The question is, why are you trying to run code inside DllMain? Is it crucial that this code run inside the context of DllMain or can you spawn a new thread and run the code in it, and not wait for the code to finish execution inside DllMain?

I believe that the problem with manged code specifically, is that running managed code might involves loading the CLR and suchlike and there's no knowing what could happen there that would result in a deadlock... I would not heed the advice of "disable this warning" if I were you because most chances are you'll find your applications hangs unexpectedly under some scenarios.

1
  • 5
    I'm working on a Direct3D application. This is an EXE. However, I am still seeing this error. Any ideas how to best fix this? Oct 16, 2008 at 10:36
23

UPDATE FOR .NET 4.0 AND MORE RECENT FRAMEWORKS

This is an old question asked at the time of .Net 2.0, when support for mixed mode DLLs had serious initialization problems, prone to random deadlocks. As of .Net 4.0, the initialization of mixed mode DLLs has changed. Now there are two separate stages of initialization:

  1. Native initialization, called at the DLL's entry point, which includes native C++ run-time setup and execution of your DllMain method.
  2. Managed initialization, executed automatically by system loader.

Since step #2 is performed outside of the Loader Lock, there is no deadlocks. The details are described at Initialization of Mixed Assemblies.

To ensure your mixed mode assembly can be loaded from a native executable, the only thing you need to check is that DllMain method is declared as native code. #pragma unmanaged could help here:

#pragma unmanaged

BOOL APIENTRY DllMain(HMODULE hModule,
    DWORD  ul_reason_for_call,
    LPVOID lpReserved
    )
{
    ... // your implementation here
}

It is also important that any code that DllMain might call directly or indirectly is also unmanaged. It makes sense to limit the type of functionality used by DllMain so you trace all code reachable from DllMain and ensure it is all compiled with #pragma unmanaged.

Compiler helps a little by giving you warining C4747 if it detects that DllMain is not declared as unmanaged:

1>  Generating Code...
1>E:\src\mixedmodedll\dllmain.cpp : warning C4747: Calling managed 'DllMain': Managed code may not be run under loader lock, including the DLL entrypoint and calls reached from the DLL entrypoint

However compiler won't generate any warnings if DllMain indirectly calls some other managed function, so you need to ensure that never happens, otherwise your application could deadlock randomly.

0
7

kindly remind those VS2017 users that you need to disable "exception helper" instead of "exception assistant"(before VS2017) to prevent from loader lock error, which setting path is Debug->Exception. Just ran int to this problem and wasted 2 hours searching for solutions...

2
  • I don't have "Exception" under "Debug". I have VS2017 Community 15.8.4
    – Alex
    Oct 11, 2018 at 14:13
  • @Alex, check for Debug-->Windows-->Exception Settings, or press Ctrl+Alt+E
    – mistika
    Jul 22, 2019 at 17:15
6

Press ctr d+e Then Expend Managed Debugging Assistants Node. Then Unchecked the LoaderLock.

Hope this will help you.

1
  • 3
    The shortcut actually depends on the configuration you have specified to use during first run. The C# shortcut layout is (Ctrl+D, E). (Also you can assign any key combination to this function in Options->Environment->Keyboard.)
    – Adam L. S.
    Jun 10, 2016 at 7:13
4

I recently got this error while creating an instance of an COM-Object written in native code:

m_ComObject = Activator.CreateInstance(Type.GetTypeFromProgID("Fancy.McDancy"));

This led to the described error. A "LoaderLock was detected"-Exception was thrown.

I overcame this error by creating the object-instance in an extra thread:

ThreadStart threadRef = new ThreadStart(delegate { m_ComObject = Activator.CreateInstance(Type.GetTypeFromProgID("Fancy.McDancy")); });
Thread myThread = new Thread(threadRef);

myThread.Start();
myThread.Join(); // for synchronization
1
  • The error can happen with Remotable objects (MarshalByRefObject) , and this solution doesn't work for those.
    – Matthieu
    Oct 16, 2019 at 14:26
3

I'm building a C++ CLR DLL (MSVS2015) that has to make calls into an unmanaged DLL and define unmanaged code. I use #pragma managed and #pragma unmanaged to control what mode it is in for a given area of the code.

In my case I simply put #pragma unmanaged in front of my DllMain() and this solved the problem. It seemed to be thinking I wanted a managed version of DllMain().

2

The setting path in my visual studio 2017 instance is Debug -> Windows -> Exception Settings . The exception settings "window" showed up in the bottom tab group (as opposed to a separate window), took me a while to notice it. Search for "loader".

0
1

This problem occurs because of the way in which the debugger in Visual Studio runs managed applications that use Microsoft Foundation Classes version 8.0 in one or more DLL files.

Have a thorough reading at: http://msdn.microsoft.com/en-us/library/aa290048(vs.71).aspx

Not the answer you're looking for? Browse other questions tagged or ask your own question.