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

Home Posts Topics Members FAQ

Native lib with singleton and C++/CLI program

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.

CLIAssembly::CL IObject::Value( 42); // Is a wrapper around
Native::SingleO bject::instance ().value(int i)
Native::SingleO bject::instance ().value(13);

Console::WriteL ine("Value in CLI object is {0}",
CLIAssembly::CL IObject::Value( ));
Console::WriteL ine("Value in Native object is {0}",
Native::SingleO bject::instance ().value());

gives as output

Value in CLI object is 42
Value in Native object is 13

Only when I put the source code of the native lib in the CLI/C++
program project I get the behaviour I'd expect.

I've tried various options:
- Compiling the native lib with /clr
- adding #pragma managed and #pragma unmanaged around the include for
the native code in the CLI project.
All to no avail.

Any help and ideas would be much appreciated.

Joes
Jul 15 '08 #1
4 2875
<jo********@gma il.comwrote in message
news:10******** *************** ***********@34g 2000hsf.googleg roups.com...
Only when I put the source code of the native lib in the CLI/C++
program project I get the behaviour I'd expect.

I've tried various options:
- Compiling the native lib with /clr
- adding #pragma managed and #pragma unmanaged around the include for
the native code in the CLI project.
All to no avail.

Any help and ideas would be much appreciated.
How is your code linked? (i.e. is it a single monolightic EXE, EXE+DLL,
multiple DLLs, etc) How is the singleton actually declared? Code
defined in a library will, of course, be duplicated in each image (DLL, EXE,
etc) that uses the library.

If you've got two different values, you have one of two problems:
1. Your wrapper isn't really wrapping what you think it is.
2. Youe singleton is actually a doubleton.

A short complete example (i.e. something that other can compile/link/run)
would be very helpful in understanding where exactly the problem lies.

-cd
Jul 15 '08 #2
On Jul 15, 9:19*pm, "Carl Daniel [VC++ MVP]"
<cpdaniel_remov e_this_and_nos. ..@mvps.org.nos pamwrote:
<joes.st...@gma il.comwrote in message

news:10******** *************** ***********@34g 2000hsf.googleg roups.com...
Only when I put the source code of the native lib in the CLI/C++
program project I get the behaviour I'd expect.
I've tried various options:
- Compiling the native lib with /clr
- adding #pragma managed and #pragma unmanaged around the include for
the native code in the CLI project.
All to no avail.
Any help and ideas would be much appreciated.

How is your code linked? *(i.e. is it a single monolightic EXE, EXE+DLL,
multiple DLLs, etc) * How is the singleton actually declared? * *Code
defined in a library will, of course, be duplicated in each image (DLL, EXE,
etc) that uses the library.

If you've got two different values, you have one of two problems:
1. Your wrapper isn't really wrapping what you think it is.
2. Youe singleton is actually a doubleton.

A short complete example (i.e. something that other can compile/link/run)
would be very helpful in understanding where exactly the problem lies.

-cd
Hi Carl,

I can post some code or a zip file somewhere with the projects
tomorrow. The code setup is as follows:
- lib with singleton (native C++ code)
- exe with a public ref wrapper class that is abstract and sealed and
two static methods that
make a call to the singleton instance from the lib and set or get a
value.

Calling the singleton via the wrapper class gives different values
than calling it directly,
as I showed in the code snippet in my initial post.

Anyway, I'll give some code tomorrow when I am back at my work
machine.

Thx,

Joes
Jul 15 '08 #3
A follow up. The issue I was encountering was the following. I have
three projects, one producing a native static lib (LIB), one a .NET
assembly (DLL) and one executable (EXE).
The LIB implements a singleton, the DLL writes a wrapper around it.
The EXE calls the singleton directly by linking in the LIB, and
indirectly by referencing the wrapper DLL. It turns out the wrapper
has its own copy of the singleton and so the EXE has two independent
singletons.

Turning the LIB into a native DLL and exporting the singleton removes
the doubleton, giving the EXE access to the same instance by calling
the wrapper as well as the direct call.

I think I quite understand that, and I also know what to do to solve
the issues I've got within my projects.

Thx,

Joes
Jul 17 '08 #4
Joes wrote:
A follow up. The issue I was encountering was the following. I have
three projects, one producing a native static lib (LIB), one a .NET
assembly (DLL) and one executable (EXE).
The LIB implements a singleton, the DLL writes a wrapper around it.
The EXE calls the singleton directly by linking in the LIB, and
indirectly by referencing the wrapper DLL. It turns out the wrapper
has its own copy of the singleton and so the EXE has two independent
singletons.

Turning the LIB into a native DLL and exporting the singleton removes
the doubleton, giving the EXE access to the same instance by calling
the wrapper as well as the direct call.

I think I quite understand that, and I also know what to do to solve
the issues I've got within my projects.
Yep - that's what I thought, based on your description. Glad you got it
sorted - you're right that the only way to have a true in-process singleton
is to implement it in a DLL.

-cd
Jul 17 '08 #5

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

Similar topics

8
3653
by: quortex | last post by:
Hi all, I have a native class which has a single instance controlled via the singleton pattern. I need to call this from both native C++ and from mixed mode visual studio 2005 c++ CLI. At the moment I have a MC++ unit test project which accesses the singleton. What I seem to be finding is that the singleton accessed directly from the unit test project is not the same instance as the singleton accessed from native code.
5
5377
by: tobias.sturn | last post by:
Hi! I have written this template for making a singleton: #define DECLARE_SINGLETON(classname) \ private: \ static classname* m_pThis; \ classname(); \ class Guard \ { \ public: \
7
6641
by: Adrian | last post by:
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.instance() in the C++/CLR DLL, the second instantiation is down to the library internally calling singleton.instance(). I'm relatively new to...
6
3198
by: toton | last post by:
Hi, If I have a singleton class based on dynamic initialization (with new ) , is it considered a memory leak? Anything in C++ standard says about it ? And little off - topic question , If the singleton is initialized as a static variable , it seems there is some threading issue . Is it the issue during singleton initialization only , or during the access also? If the singleton is per thread basis (then no more singleton though ), and...
6
10391
by: =?Utf-8?B?R29yZG8=?= | last post by:
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...
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...
1
3619
by: =?Utf-8?B?RmFiaWFu?= | last post by:
I have a native singleton in a C++/CLI dll. To compile the dll itself I put a declaration of the instance variable into the cpp file (otherwise the linker complains with a LNK 2020). I now want to use the singleton also from outside the dll project. So I just #include the header file in a file in another dll. It compiles but the linker doesn't find the static variable (LNK2001). Any suggestions?
2
1890
by: Eric Lilja | last post by:
As the topic says, I wanted to make a re-usable singleton class that could create pointers to objects with non-trivial constructors. I came up with this: #ifndef SINGLETON_HPP #define SINGLETON_HPP template<typename T> struct DefaultCreatorFunctor {
3
1807
by: stevewilliams2004 | last post by:
I am attempting to create a singleton, and was wondering if someone could give me a sanity check on the design - does it accomplish my constraints, and/or am I over complicating things. My design constraints/environment are as follows: 1) Everything is single-threaded during static initialization (as in prior to the open brace of main) 2) The environment may be multi-threaded during nominal program execution (within {} of main) 3) I...
0
9535
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10242
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10200
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
10021
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
9061
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6800
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
5582
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3744
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2931
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.