G
Guest
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
"NativeSingleton" 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.
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
"NativeSingleton" 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.