Since some days ago, I was getting the following error when closing Visual Basic 6.0 from the Visual Studio debugger (I am developing a .NET-based version of MZ-Tools for the 64-bit VBA editor of Office, and VB6 will get it too):
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.
This is a warning of the Managed Debugging Assistants (MDA) of Visual Studio.
Today I decided to investigate. Soon it was clear that it was caused by the test-runner add-in that I created to run integration tests within VB 6.0. Since the error was caused during shutdown, I removed initializations (and the corresponding cleanups) to isolate the problem and I discovered that the problem was in this method:
internal List<string> GetAddinProgIds()
{
List<string> colAddinProgIds;
colAddinProgIds = new List<string>();
foreach (AddIn objAddIn in m_objVBE.Addins)
{
colAddinProgIds.Add(objAddIn.ProgId);
}
return colAddinProgIds;
}
That method gets the registered add-ins of the VBE object (to load them in a combobox and select an add-in to run its test suites).
I soon realized that maybe I should release properly some COM object and certainly the problem was fixed:
internal List<string> GetAddinProgIds()
{
List<string> colAddinProgIds;
Addins colAddins;
colAddinProgIds = new List<string>();
colAddins = m_objVBE.Addins;
foreach (AddIn objAddIn in colAddins)
{
colAddinProgIds.Add(objAddIn.ProgId);
}
// The following statement is to prevent the following error:
// 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.
System.Runtime.InteropServices.Marshal.ReleaseComObject(colAddins);
colAddins = null;
return colAddinProgIds;
}
MSFT shouldn’t have allowed to write addins in managed languages in the first place. Thank god they are blocked in the Shell now.