Static unmanaged initialization

S

shu

I get a _CrtIsValidHeapPointer assertion when trying to have a
unmanaged static object linked to my managed project.

I have tried to reduce the problem to a very simple case:

One unmanaged .cpp file (compiled with "No Common language Runtime
Support") added to a managed .exe project. This unmanaged .cpp
declares a static object, with
its constructor and destructor.

At runtime, the constructor gets called at startup. Then, the code
enters the atexit function in order to make sure that the static
destructor gets called at program exit. And inside this function is
when I get the assertion.

I have tried the same test in a managed DLL instead of an EXE, and
everything works fine. It looks like and issue with the CRT
initialization.

I have been 2 days investigating, reading through forums, etc... Some
people have come with the solution of calling __crt_dll_initialize at
the managed main(). It works. But it gets called of course after the
static constructor, suggesting that the assertion is being solved only
because calling the function forces the linker to link something...

Anyway, it seems a bug for me. Or at least, a documentation problem.

Any help will be greatly appreciated.
 
M

Marcus Heege

shu said:
I get a _CrtIsValidHeapPointer assertion when trying to have a
unmanaged static object linked to my managed project.

I have tried to reduce the problem to a very simple case:

One unmanaged .cpp file (compiled with "No Common language Runtime
Support") added to a managed .exe project. This unmanaged .cpp
declares a static object, with
its constructor and destructor.

At runtime, the constructor gets called at startup. Then, the code
enters the atexit function in order to make sure that the static
destructor gets called at program exit. And inside this function is
when I get the assertion.

I have tried the same test in a managed DLL instead of an EXE, and
everything works fine. It looks like and issue with the CRT
initialization.

I have been 2 days investigating, reading through forums, etc... Some
people have come with the solution of calling __crt_dll_initialize at
the managed main(). It works. But it gets called of course after the
static constructor, suggesting that the assertion is being solved only
because calling the function forces the linker to link something...

Anyway, it seems a bug for me. Or at least, a documentation problem.

Any help will be greatly appreciated.

I tried to reproduce your problem based on your explanations, but without
success. Can you try to reduce your code to a minimum and share it with us?

Marcus Heege
 
S

shu

Create a CLR C++ Windows Forms Application project. Add to it an
Unmanaged.cpp with the code:

---------------------------------------------------------------------
class CUnmanaged
{
public:
CUnmanaged();
~CUnmanaged();
};

static CUnmanaged s_UnmanagedTest;

CUnmanaged::CUnmanaged()
{
}

CUnmanaged::~CUnmanaged()
{
}
-------------------------------------------------

Right-click the Unmanaged.cpp: "Properties -> C/C++ -> General ->
Compile with common language support" and set it to "No common language
runtime support".

So, with this, you have a mixed assembly with an unmanaged static
object constructor called at startup.

Thank you for looking at it!!
 
S

shu

Create a CLR C++ Windows Forms Application project. Add to it an
Unmanaged.cpp with the code:

---------------------------------------------------------------------
class CUnmanaged
{
public:
CUnmanaged();
~CUnmanaged();
};

static CUnmanaged s_UnmanagedTest;

CUnmanaged::CUnmanaged()
{
}

CUnmanaged::~CUnmanaged()
{
}
-------------------------------------------------

Right-click the Unmanaged.cpp: "Properties -> C/C++ -> General ->
Compile with common language support" and set it to "No common language
runtime support".

So, with this, you have a mixed assembly with an unmanaged static
object constructor called at startup.

Thank you for looking at it!!
 

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