25

I have a couple of MVC projects which use SQL CE 4.0 and Entity Framework. Since moving to Visual Studio 2012 I keep getting the following error (not every time, but frequently)

LoaderLock was detected

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.

The error does not occur if I go back to using VS 2010, which makes me fairly certain it is an issue with Visual Studio rather than my code, but I would like someone to confirm that for me!

Edit

The problem always seems to occur when the Dispose() method of the dbcontext is called. Here is a screenshot of the Exception Assistant:

Exception Assistant

Darren
  • 4,408
  • 4
  • 39
  • 57
  • 1
    Or, VS2012 is being more careful than VS2010 about unsafe constructs. – nneonneo Oct 15 '12 at 16:52
  • 2
    No call stack to look at so hard to help you. In general, LoaderLock tends to produce false warnings. You can turn it off with Debug + Exceptions, MDAs, LoaderLock checkbox. – Hans Passant Oct 15 '12 at 17:17
  • 1
    @Hans, there doesn't seem to be a call stack (I've added a screenshot of the Exception Assistant to my post). I assume the problem is in unmanaged code. – Darren Oct 16 '12 at 12:09
  • 1
    This also happens with Microsoft Visual Studio Express 2013 for Web using MVC projects which use SQL CE 4.0 and Entity Framework. I have been continuing (F5) and the application still works. I have not found the option to turn the message off in Express yet. – Jeremy Larter May 05 '14 at 13:04

3 Answers3

14

I switch this off. As it is warning that the application could hang, if your program doesn't hang, then you're probably fine.

The problem can be solved in the same way though, by switching off the MDA:

Debug -> Exceptions -> Managed Debug Assistants

and unchecking the LoaderLock item.

John M
  • 14,338
  • 29
  • 91
  • 143
harriyott
  • 10,505
  • 10
  • 64
  • 103
5

I also had a problem with LoaderLock when I was working with some external dll in my C# application.

  • for the .NET 3.5 I just uncheck Thrown option in Exceptions menu (Loader lock error)
  • for the .NET 4.0 I added <startup useLegacyV2RuntimeActivationPolicy="true"> in app.config
Community
  • 1
  • 1
rebeliagamer
  • 1,257
  • 17
  • 33
  • 1
    I am using this for the moment, and it worked great. Had the issue with VS 2012, v4.5 and Sql CE 4.0. Here is a link with more info: http://stackoverflow.com/questions/1604663/what-does-uselegacyv2runtimeactivationpolicy-do-in-the-net-4-config/2467255#2467255 – Andrew Aug 23 '14 at 12:32
0

It may happen with compiler warning C4747.

To avoid this, make DllMain unmanaged.

For example:

#include "pch.h"
#pragma unmanaged // <- add this line just after include to make code after this be unmanaged.
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}

or

#include "pch.h"
#pragma managed(push, off) // <- add this line before function DllMain
BOOL APIENTRY DllMain( HMODULE hModule,
                       DWORD  ul_reason_for_call,
                       LPVOID lpReserved
                     )
{
    switch (ul_reason_for_call)
    {
    case DLL_PROCESS_ATTACH:
    case DLL_THREAD_ATTACH:
    case DLL_THREAD_DETACH:
    case DLL_PROCESS_DETACH:
        break;
    }
    return TRUE;
}
#pragma managed(pop) // <- add this line after function DllMain to make code below managed.