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::CLIObject::Value(42); // Is a wrapper around
Native::SingleObject::instance().value(int i)
Native::SingleObject::instance().value(13);
Console::WriteLine("Value in CLI object is {0}",
CLIAssembly::CLIObject::Value());
Console::WriteLine("Value in Native object is {0}",
Native::SingleObject::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 4 2851
<jo********@gmail.comwrote in message
news:10**********************************@34g2000h sf.googlegroups.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
On Jul 15, 9:19*pm, "Carl Daniel [VC++ MVP]"
<cpdaniel_remove_this_and_nos...@mvps.org.nospamwr ote:
<joes.st...@gmail.comwrote in message
news:10**********************************@34g2000h sf.googlegroups.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
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
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 This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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...
|
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: \
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
|
by: aa123db |
last post by:
Variable and constants
Use var or let for variables and const fror constants.
Var foo ='bar';
Let foo ='bar';const baz ='bar';
Functions
function $name$ ($parameters$) {
}
...
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
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,...
|
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...
|
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,...
|
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...
| |