infinite loop in malloc?

J

John

Hi All,

I'm an experienced C# developer that has very little experience with
C++. Unfortunately, it fell to me to wrap some unmanaged c++ (itself
ported from ADA) to provide managed types that can be consumed by other
C# developers on my team. So! I'm trying to use Managed Extensions
(.NET Framework v1.1) to achieve my goal.

I have a C++.NET DLL that compiles and links. In it, a managed type
contains (as a private member) the unmanaged type that I am wrapping.
When, in the managed type's constructor, I attempt to new up the
unmanaged type, I crash with a stack overflow exception.

So I set some options that would allow me to debug down into MS code.
What I found was an infinite loop, which explains the stack overflow.
The problem is that I don't know what is causing the infinite loop. I
see [I think] that the code is trying and failing to lock the heap.
And here's where my lack of experience in C++ hurts me. I have no idea
why it would fail, and definitely no idea why it would infinitely
re-attempt until the stack overflows.

This type of error makes me think that I'm doing something silly due to
my lack of experience. The stack trace is shown below. Does anyone
have any idea what I may be doing wrong?

Thanks!
John

DBaseWrapper.dll!_lock(int locknum=4) Line 304 C
DBaseWrapper.dll!_nh_malloc_dbg(unsigned int nSize=24, int nhFlag=0,
int nBlockUse=2, const char * szFileName=0x1007c4e4, int nLine=251)
Line 254 + 0x7 C
DBaseWrapper.dll!_malloc_dbg(unsigned int nSize=24, int nBlockUse=2,
const char * szFileName=0x1007c4e4, int nLine=251) Line 176 + 0x1b C
DBaseWrapper.dll!_mtinitlocknum(int locknum=4) Line 251 + 0x13 C
DBaseWrapper.dll!_lock(int locknum=4) Line 311 + 0x9 C
DBaseWrapper.dll!_nh_malloc_dbg(unsigned int nSize=24, int nhFlag=0,
int nBlockUse=2, const char * szFileName=0x1007c4e4, int nLine=251)
Line 254 + 0x7 C
DBaseWrapper.dll!_malloc_dbg(unsigned int nSize=24, int nBlockUse=2,
const char * szFileName=0x1007c4e4, int nLine=251) Line 176 + 0x1b C
DBaseWrapper.dll!_mtinitlocknum(int locknum=4) Line 251 + 0x13 C
DBaseWrapper.dll!_lock(int locknum=4) Line 311 + 0x9 C
DBaseWrapper.dll!_nh_malloc_dbg(unsigned int nSize=516, int nhFlag=0,
int nBlockUse=1, const char * szFileName=0x00000000, int nLine=0) Line
254 + 0x7 C
DBaseWrapper.dll!malloc(unsigned int nSize=516) Line 139 + 0x15 C
DBaseWrapper.dll!operator new(unsigned int size=516) Line 12 +
0x9 C++
Adapter.exe!Adapter.UnmanagedDB.UnmanagedDB() Line 20 + 0x14 bytes C#
Adapter.exe!Adapter.UnmanagedDB.Main() Line 35 + 0x14 bytes C#
 
O

Oleg Starodumov

I have a C++.NET DLL that compiles and links. In it, a managed type
contains (as a private member) the unmanaged type that I am wrapping.
When, in the managed type's constructor, I attempt to new up the
unmanaged type, I crash with a stack overflow exception.

So I set some options that would allow me to debug down into MS code.
What I found was an infinite loop, which explains the stack overflow.
The problem is that I don't know what is causing the infinite loop. I
see [I think] that the code is trying and failing to lock the heap.
And here's where my lack of experience in C++ hurts me. I have no idea
why it would fail, and definitely no idea why it would infinitely
re-attempt until the stack overflows.

It looks like CRT library is not initialized.

Usually your DLL should provide initialization and cleanup functions for CRT,
and the client should call them. You can find the details in these articles:
http://support.microsoft.com/?id=814472
http://msdn.microsoft.com/msdnmag/issues/05/02/CATWork/default.aspx

Regards,
Oleg
[VC++ MVP http://www.debuginfo.com/]
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top