473,508 Members | 2,303 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

static unmanaged object in mixed exe

shu
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 .cpp to make the example shorter :

class CTest
{
public:
CTest();
~CTest(); // <- Important to reproduce
};

CTest::CTest()
{
}

CTest::~CTest()
{
}

static CTest s_CTest; // Here!!: _CrtIsValidHeapPointer

I also have 2 other additional .cpp in the project: AssemblyInfo.cpp
and Main.cpp, both compiled with the /clr switch.

My problem arises when calling the unmanaged static constructor... it
calls the unmanaged crt _atexit function in order to subscribe the
destructor to the list of functions that get called at program exit,
but it seems like the CRT is not initialized yet!!

Any ideas?

Jan 31 '06 #1
6 3336
Hello,

I find this problem a few days ago.
And I know only one resolution for this problem:

Every .cpp file which contains global variable of type with non-trivial
constructor/destructor must be compiled with /clr switch.

In my opinion, this is a compiler bug.

Best regards,
DS

shu wrote:
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 .cpp to make the example shorter :

class CTest
{
public:
CTest();
~CTest(); // <- Important to reproduce
};

CTest::CTest()
{
}

CTest::~CTest()
{
}

static CTest s_CTest; // Here!!: _CrtIsValidHeapPointer

I also have 2 other additional .cpp in the project: AssemblyInfo.cpp
and Main.cpp, both compiled with the /clr switch.

My problem arises when calling the unmanaged static constructor... it
calls the unmanaged crt _atexit function in order to subscribe the
destructor to the list of functions that get called at program exit,
but it seems like the CRT is not initialized yet!!

Any ideas?


Jan 31 '06 #2
<ds****@gmail.com> wrote
Hello,

I find this problem a few days ago.
And I know only one resolution for this problem:

Every .cpp file which contains global variable of type with non-trivial
constructor/destructor must be compiled with /clr switch.

In my opinion, this is a compiler bug.

I think we've had that one before. It's really an issue with CRT
initialization. The initialization code expects the constructors
to be encoded (with Windows EncodePointer function), which
they are not. Therefore the code passes a bogus pointer to
realloc, which asserts in debug mode. Usually, the CRT registers
an early init function which does the encoding (IIRC it's
pre_c_init). You'll notice that this function is not present
in managed exe's. I suggest you open a bug at

http://lab.msdn.microsoft.com/productfeedback/

BTW: While it might sound odd at first, that a global object has
a destructor effectively requires initialization code.

The initialization code will register the object for destruction
by calling atexit (or a similar function).

-hg
Jan 31 '06 #3
"Holger Grund" <ho**********@remove.ix-n.net> wrote
I think we've had that one before. It's really an issue with CRT
initialization. The initialization code expects the constructors
to be encoded (with Windows EncodePointer function), which
they are not. Therefore the code passes a bogus pointer to
realloc, which asserts in debug mode. Usually, the CRT registers
an early init function which does the encoding (IIRC it's
pre_c_init). You'll notice that this function is not present
in managed exe's. I suggest you open a bug at

My bad! Looking at my notes, it seemed to be a bug in the
project wizard. The wizard sets the linker option /ENTRY:main
which bypasses CRT initialization.

Can you try the following in your project:

- Project Settings/Configuration Properties/Linker/System
Clear SubSystem (select "<inherit from ...>" which should
result in "not set").
- Project Settings/Configuration Properties/Linker/Advanced
Clear entrypoint (again by selecting "<inherit from ...>")

Does this work for you?

-hg
Jan 31 '06 #4
Hello, Holger,

Yes, this work fine. BUT, this makes our application as a console
(console windows opened on start up and this is really BAD). Problem
with native globale variable occured only with Windows Forms projects.

See also
http://lab.msdn.microsoft.com/produc...6-2f4f1af866d5
You can find my test solution attached to this feedback.

Best regards,
DS

Holger Grund wrote:
"Holger Grund" <ho**********@remove.ix-n.net> wrote
I think we've had that one before. It's really an issue with CRT
initialization. The initialization code expects the constructors
to be encoded (with Windows EncodePointer function), which
they are not. Therefore the code passes a bogus pointer to
realloc, which asserts in debug mode. Usually, the CRT registers
an early init function which does the encoding (IIRC it's
pre_c_init). You'll notice that this function is not present
in managed exe's. I suggest you open a bug at

My bad! Looking at my notes, it seemed to be a bug in the
project wizard. The wizard sets the linker option /ENTRY:main
which bypasses CRT initialization.

Can you try the following in your project:

- Project Settings/Configuration Properties/Linker/System
Clear SubSystem (select "<inherit from ...>" which should
result in "not set").
- Project Settings/Configuration Properties/Linker/Advanced
Clear entrypoint (again by selecting "<inherit from ...>")

Does this work for you?

-hg


Feb 1 '06 #5
Hello,

I found new way to resolve this problem:

"- Project Settings/Configuration Properties/Linker/Advanced
Clear entrypoint (again by selecting "<inherit from ...>") " (as Holger
wrote)

+

add the following code near main() definition:

int main(array<System::String ^> ^args)
{
<...>
}

int __stdcall WinMain(int hInstance, int hPrevInstance, void
*lpCmdLine, int nShowCmd)
{
return main(gcnew array<System::String^>(0));
}
Code doesn't contain initialization of the args parameter and need to
be extended.
But, in my opinion, this is right way (no console shown).

Feb 1 '06 #6
Hello,

I found new way to resolve this problem :-) :

Just change
/entry:main
to
/entry:?mainCRTStartupStrArray@@$$FYMHP$01AP$AAVStr ing@System@@@Z

No other changes need.

Best regards,
DS

Feb 1 '06 #7

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

Similar topics

5
2925
by: Adam McKee | last post by:
We are using Visual Studio.NET 2003 in our project with .NET framework 1.1. One of our libraries is a mixed-mode dll assembly consisting of one managed C++ library, and several unmanaged C++...
6
2799
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...
4
2147
by: | last post by:
I am stuck in a situation and I do believe that this should work, but it doesn't. I have a unmanaged dll, that uses MFC. This works great. Now I recompile the unmanaged dll so it contains...
3
2625
by: Steve | last post by:
I have former VC++ 6.0 application which was ported to .NET. Now we continue to develop this app under VS.NET. The app now contains both managed and unmanaged classes. Now I have a problem with...
2
2083
by: AAguiar | last post by:
Thanks for your replies. Last week, I continued working with this problem. Trying to reproduce the error, I developed a short example and found that the problem is related to strong name dll. ...
14
2557
by: AAguiar | last post by:
Thanks for your replies. Last week, I continued working with this problem. Trying to reproduce the error, I developed a short example and found that the problem is related to strong name dll. ...
3
1372
by: shu | last post by:
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...
1
2212
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: ...
4
4502
by: Dave | last post by:
I have a global.asax file with Application_Start defined and create some static data there and in another module used in the asp.net application and I realize that static data is shared amongst...
0
7225
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
7123
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
7382
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...
1
7042
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...
1
5052
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...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3193
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3181
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1556
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

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.