473,403 Members | 2,071 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,403 software developers and data experts.

Static unmanaged initialization

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.

Jan 8 '06 #1
3 1367
"shu" <sh*******@gmail.com> wrote in message
news:11********************@g49g2000cwa.googlegrou ps.com...
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
Jan 8 '06 #2
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!!

Jan 9 '06 #3
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!!

Jan 9 '06 #4

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

Similar topics

1
by: Qin Chen | last post by:
I will present very long code, hope someone will read it all, and teach me something like tom_usenet. This question comes to me when i read <<Think in C++>> 2nd, chapter 10 , name control,...
6
by: mflanagan | last post by:
I have unmanaged C++ program that will load a managed C++ dll and then call a function in that dll. The managed C++ routine will call some C# routines. The unmanaged C++ main program will make...
12
by: Joe Narissi | last post by:
I know how to create and use static constructors, but is there a such thing as a static destructor? If not, then how do you deallocate memory intialized in the static constructor? Thanks in...
6
by: shu | last post by:
Is it legal to declare a static instance of an object compiled in a "No CLR support" .obj? I have the next .cpp, in which I mix definition of CTest and declarion of the static var in the same...
1
by: bvisscher | last post by:
I posted this recently in microsoft.public.vc.language and was redirected here. I also searched this ng and found some relavant threads. The most relavent I found was: ...
6
by: Stephen Walch | last post by:
Our application environment consists of three basic layers: 1. Third-party unmanaged DLLs that were written before the CLR was invented and maintain a significant amount of information (including...
5
by: Jesper Schmidt | last post by:
When does CLR performs initialization of static variables in a class library? (1) when the class library is loaded (2) when a static variable is first referenced (3) when... It seems that...
5
by: Jesper Schmidt | last post by:
Hi, I have a relatively large C++ code base, which requires that static initializers are called when a dll is loaded. This seems to work well for native code, but it does not work for files...
20
by: JohnQ | last post by:
The way I understand the startup of a C++ program is: A.) The stuff that happens before the entry point. B.) The stuff that happens between the entry point and the calling of main(). C.)...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
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
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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.