473,398 Members | 2,389 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,398 software developers and data experts.

Problem with MFC extension DLL, threads, and .Net loader lock

We have two separate DLLs that do not interact (directly, at least). One is
an MFC extension DLL that was built back in VC++ 6 days (but has been
recompiled with VS2005). The other is a DLL that contains .Net functions
wrapped in C++ (as described by Paul DiLascia -- see
http://msdn.microsoft.com/msdnmag/is.../default.aspx). Both
DLLs specify 'Use MFC in a shared DLL'.

Since we have an old VC 6 application (large) that we would like to start
using .Net classes with (e.g. regular expressions), we have been trying to
implement the wrapped approach, which involves creating a DLL with the
wrapped .Net stuff in it. Hope that's clear ...

What we have found is that, when we use the wrapped DLL, we blow up with a
'loader lock' issue. This only occurs, though, when we do the following:

1. Implicitly link to the MFC extension DLL (XMap).
2. Create a thread, using AfxBeginThread, that will use the XMap DLL to do
some time-consuming initialization stuff.
3. When the initialization is done, that secondary worker thread exits -- at
which point we get the following (i.e. the mda:msg -- I have included the
debug output from the point at which the XMap DLL got loaded):

XMAP.DLL Initializing!
'CAD.exe': Loaded
'D:\views\al08333_view_SS_9_3_Dev\xwaveCAD\Worksta tion\ciids96\debug\Splash.dll',
Symbols loaded.
'CAD.exe': Unloaded
'D:\views\al08333_view_SS_9_3_Dev\xwaveCAD\Worksta tion\ciids96\debug\Splash.dll'
'CAD.exe': Loaded
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\msc orwks.dll', Symbols loaded
(source information stripped).
'CAD.exe': Loaded
'C:\WINDOWS\WinSxS\x86_Microsoft.VC80.CRT_1fc8b3b9 a1e18e3b_8.0.50727.91_x-ww_0de56c07\msvcr80.dll',
Symbols loaded (source information stripped).
CDEQView::OnInitialUpdate() called
Comms IndicationHandler set to CWorkstation::HandleIndications
'CAD.exe': Loaded
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Cul ture.dll', Symbols loaded
(source information stripped).
'CAD.exe': Unloaded
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Cul ture.dll'
'CAD.exe': Loaded
'C:\WINDOWS\assembly\NativeImages_v2.0.50727_32\ms corlib\467bb10fb591904e8c6509fc55279db2\mscorlib.n i.dll',
No native symbols in symbol file.
'CAD.exe': Loaded
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Cul ture.dll', Symbols loaded
(source information stripped).
'CAD.exe': Unloaded
'C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727\Cul ture.dll'
<mda:msg xmlns:mda="http://schemas.microsoft.com/CLR/2004/10/mda">
<!--
DLL 'c:\tools\RegexWrapD.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.
-->
<mda:loaderLockMsg break="true"/>
</mda:msg>
CAD.exe has triggered a breakpoint

If I don't allow the thread to exit (by putting an INFINITE wait on a handle
right before the return statement), everything works!

It appears as though when tearing the thread down, MFC gets confused about
the wrapped DLL (RegexWrapD.dll). Where I get confused is that neither DLL
is explicitly aware of the other (i.e. neither one calls any functions in
the other).

When looking at the stack backtrace, the 'green arrow' in the backtrace
points to the following ATL source:

In module dllmodul.cpp, function:

extern "C"
BOOL WINAPI InternalDllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
/*lpReserved*/)
error (green arrow) occurs at line 195:

else if (dwReason == DLL_THREAD_DETACH)
{
#ifdef _AFXDLL
AFX_MANAGE_STATE(&afxModuleState); //// LINE 195 -- ERROR
HERE!
#endif

The MFC extension DLL has a DllMain, which is supposed to be supported in
VS2005. Here is what is does:

static AFX_EXTENSION_MODULE NEAR XMapDLL = { NULL, NULL };

extern "C" int APIENTRY
DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
{
// Remove this if you use lpReserved
UNREFERENCED_PARAMETER(lpReserved);

if (dwReason == DLL_PROCESS_ATTACH)
{
// Extension DLL one-time initialization
if (!AfxInitExtensionModule(XMapDLL, hInstance))
return 0;
new CDynLinkLibrary(XMapDLL);
}
else if (dwReason == DLL_PROCESS_DETACH)
{
//TRACE0("XMAP.DLL Terminating!\n");
// Terminate the library before destructors are called
AfxTermExtensionModule(XMapDLL);
}
return 1; // ok
}

We have tried creating a separate initialization function that get's called
explicitly by the thread(s), and putting the 'new CDynLinkLibrary' call in
it, but that does not work either (i.e. calling it once from the main thread
only, or calling it twice (once from the main thread, once from the worker
thread). I have also created a dummy MFC extension DLL using the VS2005
wizard and I can access a function within it fine from 2 different threads.
The only thing about this dummy MFC extension DLL is that I don't allocate
any MFC objects, whereas the XMap DLL does (both global, static objects like
CPtrArray and other objects created by existing initialization calls from
the application).

Any thoughts on the problem?

Thanks

Al
Oct 26 '06 #1
4 5775
Hi Norman,

Based on my understanding, the problem is your application implicitly links
to a MFC extension DLL(XMap), and use that DLL's function in a worker
thread. When that worker thread exits, yo receive an error message about
the OS Load Locker issue which invoked from another mixed DLL
(RegexWrapD.dll). Please correct me if I misunderstood anything.

Before we dig into this problem, would you please tell us whether this
problem occurs outside the VS2005 debugger? If your application could work
well without VS2005' s debugger, then this problem may occur because of the
way in which the debugger in Visual Studio 2005 runs managed applications
that use Microsoft Foundation Classes version 8.0 in one or more DLL files.
We have an MSDN KB article which targets on this issue:

FIX: Error message when you use the debugger in Visual Studio 2005 to debug
an MFC class library application: "Managed Debugging Assistant 'LoaderLock'
has detected a problem in '<filename.exe>'"
http://support.microsoft.com/default.aspx/kb/913996

If the above KB article cannot resolve your problem, we may need more info
to isolate the issue:

1. How about that mixed DLL, how do you use it in your application? It
seems that worker thread also load that DLL.

2. Each thread's call stack info while the application breaks on that
error. You can get a specific thread's call stack by select the target
thread ID in the Debug | Windows | Threads window, then invoke its
corresponding Call Stack window.

Before doing this, you may need to setup the debugging symbols for your
VS2005. For example to set the Symbol Server path you need to input
"srv*c:\LocalSymbols*http://msdl.microsoft.com/download/symbols;" in the
Tools | Options | Debugging | Symbols as the directory. This will tell the
VS2005 debugger to lookup symbol from the Microsoft symbol server
"http://msdl.microsoft.com/download/symbols" and cache the downloaded
symbol in "c:\LocalSymbols" folder.

By the way, there would be many reasons to cause the loader locker problem
in the case of the mixed DLL, I suggest you refer to the following MSDN
articles for the details:

Converting Managed Extensions for C++ Projects from Pure Intermediate
Language to Mixed Mode
http://msdn.microsoft.com/library/de...us/vcmex/html/
vcconconvertingmanagedextensionsforcprojectsfrompu reintermediatelanguagetomi
xedmode.asp

Mixed DLL Loading Problem
http://msdn.microsoft.com/library/de...us/dv_vstechar
t/html/vcconMixedDLLLoadingProblem.asp
Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscripti...t/default.aspx.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 27 '06 #2
Actually, my first name is 'Al' -- Norman is my last name ... but I'll
respond to anything!

My comments are interspersed with your text below ...
>
Based on my understanding, the problem is your application implicitly
links
to a MFC extension DLL(XMap), and use that DLL's function in a worker
thread. When that worker thread exits, yo receive an error message about
the OS Load Locker issue which invoked from another mixed DLL
(RegexWrapD.dll). Please correct me if I misunderstood anything.
That is correct.
Before we dig into this problem, would you please tell us whether this
problem occurs outside the VS2005 debugger? If your application could work
well without VS2005' s debugger, then this problem may occur because of
the
way in which the debugger in Visual Studio 2005 runs managed applications
that use Microsoft Foundation Classes version 8.0 in one or more DLL
files.
We have an MSDN KB article which targets on this issue:

FIX: Error message when you use the debugger in Visual Studio 2005 to
debug
an MFC class library application: "Managed Debugging Assistant
'LoaderLock'
has detected a problem in '<filename.exe>'"
http://support.microsoft.com/default.aspx/kb/913996
Yep. That fixed the problem -- I see the KB article requires a hot fix from
Microsoft. Should I get that or wait for the SP1 for VS2005? How soon is
that coming?

<rest of reply snipped>

I had already read/done the rest of your suggestions. I like the fact that
VS2005 and Debuggging Tools for Windows seem to be sharing a common debugger
......

Al Norman
Oct 27 '06 #3
Hi Al,
>Yep. That fixed the problem -- I see the KB article requires a hot fix
from Microsoft. Should I get that or wait for the SP1 for VS2005?
I am glad to know that KB helps! The VS2005 SP1 is still in beta test
stage, so I suggest you contact our CSS(Microsoft Customer Support Service)
for the hotfix directly. You need to tell them the corresponding KB number
for it.

Since this problem is a product issue, CSS won't charge money. Every MSDN
subscriber has two free support incidents. You could use one of them to
contact Microsoft CSS. So you don't need to pay money yet, you still have
two free support incidents.

For your reference, I have attached steps to contact Microsoft CSS here:
You can contact Microsoft Customer Support Service directly to discuss
additional support options you may have available, by contacting us at
1-(800)936-5800 or by choosing one of the options listed at:

http://support.microsoft.com/common/...gp;en-us;offer
prophone

I hope the above information helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no rights.

Oct 30 '06 #4
No problems. Thanks for the help.

Al

""Gary Chang[MSFT]"" <v-******@online.microsoft.comwrote in message
news:Ls******************@TK2MSFTNGXA01.phx.gbl...
Hi Al,
>>Yep. That fixed the problem -- I see the KB article requires a hot fix
from Microsoft. Should I get that or wait for the SP1 for VS2005?

I am glad to know that KB helps! The VS2005 SP1 is still in beta test
stage, so I suggest you contact our CSS(Microsoft Customer Support
Service)
for the hotfix directly. You need to tell them the corresponding KB number
for it.

Since this problem is a product issue, CSS won't charge money. Every MSDN
subscriber has two free support incidents. You could use one of them to
contact Microsoft CSS. So you don't need to pay money yet, you still have
two free support incidents.

For your reference, I have attached steps to contact Microsoft CSS here:
You can contact Microsoft Customer Support Service directly to discuss
additional support options you may have available, by contacting us at
1-(800)936-5800 or by choosing one of the options listed at:

http://support.microsoft.com/common/...gp;en-us;offer
prophone

I hope the above information helps, if you have any questions or concerns,
please do not hesitate to let me know. I am standing by to help you.

Thanks!

Best regards,

Gary Chang
Microsoft Online Community Support
==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscripti...ult.aspx#notif
ications.
==================================================
This posting is provided "AS IS" with no warranties, and confers no
rights.

Oct 30 '06 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: Torsten Mohr | last post by:
Hi, i write an extension module in C at the moment. This module does some work on some own data types that consist of some values. The functions that can change the data are written in C. ...
5
by: Leonardo Hyppolito | last post by:
Hello! I am trying to implement a program that uses Threads. I chose the producers and consumers scenario. The producers put a "product" (which is an int number) in a shared storage place of one...
13
by: Jason Jacob | last post by:
To all, I have a GUI program (use c#), and I have create a Thread for loading some bulk data, I also arrange the GUI program like this: 1) load a form showing "Wait for loading..." etc 2) a...
2
by: Craig Klementowski | last post by:
Pardon the cross post, but I'm not sure where exactly to post this question. We have MFC application using many MFC extention DLL's. We started using a new MFC extention DLL that is mixed mode so...
4
by: Marko Becirevic | last post by:
Hi, does anyone know how to bypass bug in .NET framework where NT loader deadlock occurs? It seems to me that using methods like Process.GetCurrentProcess is not safe, and also using mixed dlls...
4
by: Madhu Gopinathan | last post by:
Hi All, I am faced with a horrible hang problem. I have a COM exe server that executes some tasks. The task execution manager is a thread that manages the pool of threads, which is 4 per processor....
6
by: Wilfried Mestdagh | last post by:
Hi, I'm trying out the demo version of FastReports. All is running fine in a small test project. I just copy my code from test project into a large application to demonstrate tomorrow but have...
5
by: jeff.ranney | last post by:
Hi all. I wrote this C# code: lock(this) { / /write to a file }
13
by: AliRezaGoogle | last post by:
Dear Members, I have a problem in the concepts of Monit.Enter and Monitor.Exit (Before everything I should say that I know how to solve this problem by using Monitor.Wait and I do not need a...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.