
Singletons & delete error
From a first glance, this looks like a memory allocation/deallocation
problem across DLL boundaries. Memory management in Win32 DLLs is a
veritable pain in the ass. But to cut a long story short, to avoid problems,
you need to allocate and deallocate objects implemented in the DLL *in the
context of the DLL itself* (for various reasons, including different heaps,
packing, RTL, etc., some or all of which may or may not apply here).
Normally, this is done by providing factory objects or methods as part of
the object or the DLL, along with matching code that performs deallocation.
class SingletonImpl : public Singleton
{
.
.
.
static Singleton *Create() { return new Singleton; }
static void Destroy(Singleton *s) { delete s; }
.
.
.
Quote:
};
This will ensure the object is created and destroyed within the context of
the DLL. Also, your code mixes instantiating DLL-provided objects on your
main process stack with heap-based allocations. Switch to using only the
factory methods above instead.
-- Dan
Quote:
> The following code works if everything is contained in the
> executable. However, if the Singleton is put in its own
> DLL, I have the problem that when I create the Singleton
> via a "new Singleton" call in the main routine, the delete
> function will cause the function to crash. Any ideas as
> to why? Note, one can still use the "new" singleton and
> have it work, one can just not delete it.
> -Jeff
> Code was pulled from:
> http://www.codeguru.com/mfc/comments/1081.shtml
> //---------- the h file ---------------------------------
> class Singleton
> {
> public:
> Singleton();
> virtual ~Singleton() {}
> virtual void Method1() { h.Method1(); }
> virtual int Method2() { return h.Method2(); }
> private:
> Singleton& h; // The handle
> };
> //--------------- The cpp implementation file -----------
> #include "Singleton.h"
> class SingletonImpl : public Singleton
> {
> public:
> SingletonImpl() : a1(5) {}
> virtual void Method1();
> virtual int Method2();
> static Singleton& Get();
> private:
> int a1; // implementation details ...
> void Internal2(); // This could be public as well
> };
> Singleton::Singleton()
> : h(SingletonImpl::Get()) {} // Singleton
> constructor
> // SingletonImpl implementataion
> Singleton& SingletonImpl::Get()
> {
> static SingletonImpl s;
> return s;
> }
> void SingletonImpl::Method1() { a1 *= 10; }
> int SingletonImpl::Method2() { return a1; }
> //--------------------- A sample client -------------------
> -
> #include "Singleton.h"
> int main(int argc, char* argv[])
> {
> Singleton* s1 = new Singleton;
> Singleton s2;
> s1->Method1();
> printf("Should be 50, %d\n", s2.Method2());
> printf("Should also be 50, %d\n",
> Singleton().Method2());
> delete s1;
> return 0;
> }