473,796 Members | 2,664 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Singleton in native library with C++/CLI

Hi,
I have a large unmanaged static C++ library which I've wrapped using a small
C++/CLR DLL. This is called from a C# client application.

The static library has a singleton, however it appears that it is being
instantiated twice. The first instantiation is down to me calling
singleton.insta nce() in the C++/CLR DLL, the second instantiation is down to
the library internally calling singleton.insta nce().

I'm relatively new to C++/CLR, is this expected behaviour and is there
anything I can do to work around this?

Thanks,
Adrian
Jul 11 '06 #1
7 6640
Adrian wrote:
Hi,
I have a large unmanaged static C++ library which I've wrapped using a small
C++/CLR DLL. This is called from a C# client application.

The static library has a singleton, however it appears that it is being
instantiated twice. The first instantiation is down to me calling
singleton.insta nce() in the C++/CLR DLL, the second instantiation is down to
the library internally calling singleton.insta nce().

I'm relatively new to C++/CLR, is this expected behaviour and is there
anything I can do to work around this?
I've just converted a small DLL from C++Builder to a C++/CLI DLL in
vs2005 that uses singletons with no real problems. It isn't
instantiated twice for me. The only issue I had was with cleanup. To
destroy the singleton we use std::atexit to register a cleanup func but
in C++/CLI, you have to call _onexit_m (found this because of crashes at
shutdown).

Cheers

Russell
Jul 12 '06 #2
"Russell Hind" <un*****@unknow n.comwrote in message
news:OR******** ******@TK2MSFTN GP03.phx.gbl...
I've just converted a small DLL from C++Builder to a C++/CLI DLL in vs2005
that uses singletons with no real problems. It isn't instantiated twice
for me. The only issue I had was with cleanup. To destroy the singleton
we use std::atexit to register a cleanup func but in C++/CLI, you have to
call _onexit_m (found this because of crashes at shutdown).
Hi,
Unfortunately my situation is more complex than that - I'm calling an
existing static C++ class library which is for all purposes too complex to
convert to C++/CLI from a C++/CLI program.

I've managed to reproduce the problem in a small VS2005 project - I've
uploaded it at http://www.aonaware.eclipse.co.uk/te...gletonTest.zip if
anyone has the time to look. You can see / debug the constructor of the
singleton being called twice. I think it may have something to do with the
template base class for the singleton but I'm not sure...

Thanks,
Adrian


Jul 13 '06 #3
Adrian wrote:
I've managed to reproduce the problem in a small VS2005 project - I've
uploaded it at http://www.aonaware.eclipse.co.uk/te...gletonTest.zip if
anyone has the time to look. You can see / debug the constructor of the
singleton being called twice. I think it may have something to do with the
template base class for the singleton but I'm not sure...
That's interesting. I tried your code, and the constructor is really
called twice.

However, I don't understand why your Settings::inter nalFunction is
static. Since Settings is a singleton class, it's not really needed to
make any of its members static. I modified your code this way:

Settings.hpp:
void internalFunctio n();

Settings.cpp:
void Settings::inter nalFunction()
{
getProperty();
}

Wrapper.h:
static void doSomething() {
Settings::insta nce().internalF unction();
}

And now it works fine, the constructor is only called once.

I also took a look at your project settings, and noticed that your
SingletonTest.l ib is NOT compiled with the /clr. Mixing managed and
unmanaged code can somtimes bring up very subtle problems, as you can
read it at Marcus Heege's blog:

http://www.heege.net/blog/default.as...9-9da8ec9e091e

Read "Avoid native code in managed object files (".obj" files)"

I'm not sure if your problem is related to this in any way, but guess
what. I recompiled your SingletonTest.l ib with the /clr settings, then
the rebuilt the dll and the C# code, and the problem disappeared.

So I gave you 2 different workarounds for your problem.

I'm not sure if this glitch is considered a compiler bug, or simply bad
practice, but it certainly looks serious to me.

Tom
Jul 13 '06 #4
Tamas Demjen wrote:
>
Read "Avoid native code in managed object files (".obj" files)"

I'm not sure if your problem is related to this in any way, but guess
what. I recompiled your SingletonTest.l ib with the /clr settings, then
the rebuilt the dll and the C# code, and the problem disappeared.

So I gave you 2 different workarounds for your problem.

I'm not sure if this glitch is considered a compiler bug, or simply bad
practice, but it certainly looks serious to me.
Could it be that the singleton is initialised once for native code and
once from managed code? Perhaps there is an issue with statics called
from both?

Cheers

Russell
Jul 14 '06 #5
Adrian wrote:
I've managed to reproduce the problem in a small VS2005 project
I know exactly what's wrong with your project. When you compile a
project with /clr, the default is #pragma managed. When you compile it
without /clr, the default is #pragma unmanaged. Now you can mix the two
things, but you have to make sure that the *same* piece of header file
is compiled with the same settings.

In your project, when you #include "Settings.h pp" and "Singleton.hpp" ,
the same declaration is compiled as #pragma unmanged in the unmanaged
LIB, and with #pragma managed in the managed DLL. The two things are
*not* byte compatible! You're compiling the same piece of code with
completely incompatible ways, and then link to two things together.

The simplest fix to your problem is to go to Wrapper\Stdafx. h, and
modify it to the following:

#pragma unmanaged
#include "Settings.h pp"
#pragma managed

Please forget my previous message that I sent yesterday. It's not that
it's not correct, but it doesn't show your real problem. Your real
problem is explained in this message.

Just to clarify things. You are not normally required to compile every
native-style class with #pragam unmanaged. You can very easily compile
ISO C++ classes into managed code:

#pragma managed
class NativeClass { }; // generates managed code

However, you're including a non-/clr header file to your /clr project.
You really have to make sure that the compiler settings are matching.

This is the same kind of mistake as putting #ifdef _DEBUG into your
header file, and linking your library project without _DEBUG defined,
but your main application with _DEBUG defined. When the same header file
is compiled with different compiler options, you're calling for trouble.
You can easily lose byte compatibility. This is exactly what happened
with your project -- Singleton.hpp was compiled with one compiler
setting here, and another compiler setting there, and then the two
things were linked together.

Now I can tell for sure that this was definitely not the compiler's
fault. This is something you have to watch carefully when mixing
unmanaged and managed units.

Hope this helps.

Tom
Jul 14 '06 #6
Russell Hind wrote:
Could it be that the singleton is initialised once for native code and
once from managed code?
Exactly. Thanks for the hint -- now I see that the same include file was
compiled into managed code in one project, and into unmanaged code in
the another project. Then the two object files were linked together,
which caused one function call to use a different singleton than the
other function call.

Tom
Jul 14 '06 #7

"Tamas Demjen" <td*****@yahoo. comwrote in message
news:OX******** ******@TK2MSFTN GP03.phx.gbl...
>
Please forget my previous message that I sent yesterday. It's not that
it's not correct, but it doesn't show your real problem. Your real problem
is explained in this message.
Excellent - thank you very much for this post - it explains my problem
exactly.

Thanks again.

Jul 15 '06 #8

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

Similar topics

7
12480
by: Tim Clacy | last post by:
Is there such a thing as a Singleton template that actually saves programming effort? Is it possible to actually use a template to make an arbitrary class a singleton without having to: a) explicitly make the arbitrary class's constructor and destructor private b) declare the Singleton a friend of the arbitrary class
3
2500
by: Alicia Roberts | last post by:
Hello everyone, I have been researching the Singleton Pattern. Since the singleton pattern uses a private constructor which in turn reduces extendability, if you make the Singleton Polymorphic what sort of problems/issues should be considered? Also, I see that a singleton needs to be set up with certain data such as file name, database URL etc. What issues are involved in this, and how would you do this? If someone knows about the...
6
3055
by: ank | last post by:
Hi, I have some question about Singleton Pattern in C++. I think I understand that static initialization order across translation unit is unspecified by the standard, but what can I do to ensure that some kind of static mutex is automatically initialized before the first use of it? I cannot assume that this mutex is used only after executing main()
2
2422
by: baba | last post by:
Hi all, I'm quite new to C#. I am trying to implement some basics reusable classes using this language and the .NET Framework technology. What I'm trying to do now is to implement a singleton class. I did have a look at the Microsoft "Patterns and Practices" article "Implementing Singleton in C#" (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnpatterns/html/ImpSingletonInCsharp.asp)
2
3901
by: mma | last post by:
hello everyone, i have a solution that contains the following project types: 1. Class library contains a singleton class that handles the data access (sql server 2005). 2. Windows service. uses the Class library's singleton class to manipulate data in the data base server. 3. Windows application.
6
2087
by: Palvinder Singh | last post by:
Hello google group peeps, I am new to remoting, but have a grasp of it. I am trying to create a server/client application, which will be deployed over an intranet. I have upwards of five clients that connect to a main server. The client application sets the state of several switchs (on/off by means of clicking a button). When a switch is turned on/off, its state is updated on the server, which will then raise an event to update...
1
6290
by: =?Utf-8?B?RmFiaWFu?= | last post by:
Hello, I want to give multiple native classes (in different mixed mode dlls) access to a managed output window (for error messages). Therefore I wrote a native singleton with __declspec (dllexport). I get the following compiler errors: Error 224 error C3389: __declspec(dllexport) cannot be used with /clr:pure or /clr:safe Error 225 error C4394: 'ErrorHandler::instance' : per-appdomain symbol should not be marked with...
29
1748
by: Ugo | last post by:
Hi guys, how do you make a singleton access class? Do you know a better way of this one: var singletonClass = (function( ) { // Private variable var instance = null;
4
2875
by: joes.staal | last post by:
Hi, I know this has been asked earlier on, however, none of the other threads where I looked solved the following problem. 1. I've got a native C++ library (lib, not a dll) with a singleton. 2. I've got a C++/CLI program with a wrapper around some functions in the singleton of the native lib. 3. When I run my program, the wrappers instantiate their own copy of the singleton, i.e.
0
9685
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
1
10190
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
10019
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...
1
7555
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6796
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
5447
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
5579
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4122
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
3
2928
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.