473,703 Members | 2,406 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C++/CLI WinForms app + native static library with singletons = cra

Hello everyone,
I've been trying for some time now to move to C++/CLI, but I have several
large legacy C++ static libraries I need to use. When I set up a simple
solution with a C++/CLI Winforms app and a C++ native static library, they
work well together _unless_ I have, it seems, any static variables defined in
any function in the native library.
The libraries I'm trying to use all have Meyers Singletons in them, so they
need to have static variables in some functions. The libraries depend on
having singletons with destructors that work, hence the need for Meyers
singletons.

I've reduced my sample app to a very simple one that shows the crashes I
get, and I'd be happy to send that to anyone who wants it.
Here's the essential code, though.

The WinForms main app has just one button, whose click function contains this:
NativeSingleton & s = NativeSingleton ::Instance();
(there's more, but the app crashes on this first line.)

The native library's cpp file contains this:
NativeSingleton & NativeSingleton ::Instance()
{
// This function (and class) uses a Meyers Singleton so its destructor
will
// work properly (not thread safe as written).
static NativeSingleton theSingleton;
return theSingleton;
}
....and as usual for a singleton, it's header hides the constructor for
"NativeSingleto n" and declares Instance() as static.
I think that code summarizes my problem; I'd be happy to go into more detail
if anyone wants it.

Thanks very much for _any_ insight anyone can give me.
May 4 '07 #1
6 10374
Hi Gordo,

Can you tell me what error do you get during the crash? Anyway, since you
have a little sample project, please feel free to attach it in the
newsgroup through Outlook Express or send it to me at:
je***@online.mi crosoft.com(remove "online."). I think the sample project
will provide more information about this problem.

Thanks.

Best regards,
Jeffrey Tan
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.

May 7 '07 #2
Thanks for your help, Jeffrey.
I realized later that I should have said more about the error, which is an
interesting/puzzling one.

When I click the button I get a "Debug Assertion Failed!" in dbgheap.c on
the expression "_CrtIsValidHea pPointer(pUserD ata)". When I then break
execution in the debugger and look at the call stack, I see this (snipped
down to what I think are the relevant lines):
msvcr80d.dll!_m size_dbg(void * pUserData=0x1ec 966bf, int nBlockUse=2) Line
1473 + 0x30 bytes C++
msvcr80d.dll!_d llonexit_nolock (int (void)* func=0x1e895def , void (void)* * *
pbegin=0x0013eb a0, void (void)* * * pend=0x0013eb98 ) Line 295 + 0xd bytes C
msvcr80d.dll!__ dllonexit(int (void)* func=0x1e895def , void (void)* * *
pbegin=0x0013eb a0, void (void)* * * pend=0x0013eb98 ) Line 273 + 0x11 bytes C
ManagedApp.exe! _onexit(int (void)* func=0x00403b50 ) Line 110 + 0x1b bytes C
ManagedApp.exe! atexit(void (void)* func=0x00403b50 ) Line 127 + 0x9 bytes C
ManagedApp.exe! NativeSingleton ::Instance() Line 16 + 0x32 bytes C++
[Managed to Native Transition]
ManagedApp.exe! ManagedApp::For m1::button1_Cli ck(System::Obje ct^ sender =
0x0013ed28, System::EventAr gs^ e = 0x0013ed3c) Line 87 + 0x5 bytes C++

So, as you can see, for some reason as soon as the singleton's Instance()
method is called, the app seems to want to exit (I assume that's why there
are calls to "atexit" and "_onexit". That's as far as my limited debugger
skills can take me. :-)
I'll e-mail the project to you and cross my fingers that you can make some
sense out of this.

Even if you can't, thanks very much for your time.
Gordo
""Jeffrey Tan[MSFT]"" wrote:
Hi Gordo,

Can you tell me what error do you get during the crash? Anyway, since you
have a little sample project, please feel free to attach it in the
newsgroup through Outlook Express or send it to me at:
je***@online.mi crosoft.com(remove "online."). I think the sample project
will provide more information about this problem.

Thanks.

Best regards,
Jeffrey Tan
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.

May 7 '07 #3
Hi Gordo,

Thanks for your sample project. Yes, I can reproduce this error by running
it.

I am not sure of the reason why the compiler will emit a "atexit" calling
after calling the static constructor. However, it seems that this is a
known issue which is reported by some other customer:
"static object in unmanaged code causing crash with Windows Forms"
https://connect.microsoft.com/Visual...ck.aspx?Feedba
ckID=101712

To resolve this problem, you may try to turn on the /clr compiler option in
the "NativeLib" project. To set the /clr compiler option in the Visual
Studio development environment, follow the steps below:
1. Open the project's Property Pages dialog box. For details, see How to:
Open Project Property Pages.
2. Click the Configuration Properties folder.
3. Click the General property page.
4. Modify the Common Language Runtime support property.

After turnning on the /clr option, the compiler will not emit "atexit"
calling. I am also trying to contact some C++/CLI experts internal to
understand why atexit will be generated for non /clr project. I hope they
can share some insight for the root cause. I will post any information here
ASAP. Thanks.

Best regards,
Jeffrey Tan
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.

May 8 '07 #4
Hi Gordo,

Sorry for letting you wait.

By further discussing with a developer from VC++ team, he confirmed that
this was a known issue. The main problem here is that the IDE is specific
the entry of ManagedApp as "main". However, using "main" will bypass a lot
of CRT's startup initialization. The fix here is to simply change the
entry point in the IDE. Under ManagedApp -properties-link -advanced,
change "entry point" from "main" to
"?mainCRTStartu pStrArray@@$$FY MHP$01AP$AAVStr ing@System@@@Z" . That should
re-enable the CRT startup code which initializes the internal CRT variables.

That symbol is really the mangled name for "int __clrcall
mainCRTStartupS trArray(cli::ar ray<class System::String ^ >^)".

D:\repro\onexit >undname
?mainCRTStartup StrArray@@$$FYM HP$01AP$AAVStri ng@System@@@Z
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.
Undecoration of :-
"?mainCRTStartu pStrArray@@$$FY MHP$01AP$AAVStr ing@System@@@Z" is :- "int
__clrcall mainCRTStartupS trArray(cli::ar ray<class System::String ^ >^)"

This symbol should always exist in the CRT (at least in version 8 and 9) ,
so it should be okay for you specify it as entry point.

Hope it helps.

Best regards,
Jeffrey Tan
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.

May 9 '07 #5
Wow! Thanks very very much for tracking that problem down, Jeffrey. I've been
trying various experiments on my own for many weeks, so to me it was a very
short wait. :)

That solution works like a charm, so I'm going to go try it in my production
code right away.

You've definitely made my day. :-)

Best regards,
Gordo
""Jeffrey Tan[MSFT]"" wrote:
Hi Gordo,

Sorry for letting you wait.

By further discussing with a developer from VC++ team, he confirmed that
this was a known issue. The main problem here is that the IDE is specific
the entry of ManagedApp as "main". However, using "main" will bypass a lot
of CRT's startup initialization. The fix here is to simply change the
entry point in the IDE. Under ManagedApp -properties-link -advanced,
change "entry point" from "main" to
"?mainCRTStartu pStrArray@@$$FY MHP$01AP$AAVStr ing@System@@@Z" . That should
re-enable the CRT startup code which initializes the internal CRT variables.

That symbol is really the mangled name for "int __clrcall
mainCRTStartupS trArray(cli::ar ray<class System::String ^ >^)".

D:\repro\onexit >undname
?mainCRTStartup StrArray@@$$FYM HP$01AP$AAVStri ng@System@@@Z
Microsoft (R) C++ Name Undecorator
Copyright (C) Microsoft Corporation. All rights reserved.
Undecoration of :-
"?mainCRTStartu pStrArray@@$$FY MHP$01AP$AAVStr ing@System@@@Z" is :- "int
__clrcall mainCRTStartupS trArray(cli::ar ray<class System::String ^ >^)"

This symbol should always exist in the CRT (at least in version 8 and 9) ,
so it should be okay for you specify it as entry point.

Hope it helps.

Best regards,
Jeffrey Tan
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.

May 9 '07 #6
Hi Gordo,

Thank you for the kindly word.

FYI: I have further discussed with a senior development lead of VC++ team,
he believes that if we clear up the "entry point" under ManagedApp ->
properties-link -advanced, it should pick up the right init function
(i.e. the mainCRTStartupS trArray symbol we talked about). However, further
test shows that this actually does not work.

Finally, he confirmed this as a problem of the VS2005 and will be fixed in
Orcas(next version of VS IDE). Thanks.

Best regards,
Jeffrey Tan
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.

May 10 '07 #7

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

Similar topics

2
3185
by: free2cric | last post by:
Hi, I have two c++ static libraries say A.lib and B.lib. I am creating third static library C I create project in microsoft visual studio as win32 static library. I go to project-> settings -> link , i dont find object/library module where I can type in the names of A.lib and b.lib.. this option is available if i create dynamic library. Thanks cric
1
6262
by: Vijai Kalyan | last post by:
I am not sure if this OT, but I will go ahead and post. If it has to be posted elsewhere let me know. -vijai. ------ I have a class declared as follows: #pragma once // a simple reference counter implementation
3
10958
by: mpatnam | last post by:
I have an executable which links to a static library (.a). I want to provide a hook by overriding a function part of this static library. Eg: I have a function "int blkstart(int i)" in this static library. I want to override this by having another function which is exactly similar in signature including name. When somebody call this function, control should automatically come to my overriddent function. Inside this function, based on...
4
3547
by: newest newbie | last post by:
I'm new to using c#, but what I'm trying to do is create a library control containing a form which in turn contains a Windows application that I have defined in another library. I'm able to do it if my application which I put in the form is a c++ dll like so: private extern static void RunMyApp(IntPtr handle);
8
3019
by: Robert A Riedel | last post by:
I have an application that requires a DLL and an executable that uses the DLL, both of which were implemented in Visual C++ using unmanged code. Both the executable and the DLL are linked with functions that are stored in a static library. During initialization of the executable, classes and static globals within functions linked from the static library appear to be initialized twice, once when the executable initializes the run-time code,...
3
1587
by: Manny Silva | last post by:
Hi, I would like to create a static library that uses and in effect adds to another static library. I could simply add functionality to the existing library, but functionally it doesn't really belong there... that is, the one library is very inspecific. The Library I need to create will be project specific, and I prefer not to have to mix the two... in any case, I tried to create a library that referenced the first library. I added...
15
2314
by: Notre Poubelle | last post by:
Hello, I have a large legacy MFC application. As is typical, there is an executable along with several MFC DLLs. One of these DLLs is created by staticly linking in many libraries resulting in one very large DLL that has the bulk of the code. I've downloaded the MFCWinFormsSample.EXE and have noticed that the main app could stay as a native executable (i.e. no CLR support) whereas the various ..DLLs can be marked as having CLR...
1
5341
by: Pranesh Joshi | last post by:
Hi, Does anyone know whether or not we can link a static library to a .NET project and use the classes defined in the library? The static library is built from C++ code base. Thanks, -PJ.
4
1972
by: sealo | last post by:
Hello, I have a test in VS2005 that static library A.lib use the dynamic library C.dll. Then a application App use the A.lib. App-->A.lib-->C.dll It works. But if I remove the C.dll, the app tell me can not find the C.dll. I think because A.lib is a static library, so it should contain the
0
9246
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9005
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8961
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5922
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4425
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4680
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3115
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 we have to send another system
2
2440
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2058
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.