troubleshooting Question

Templatized singleton gets created twice!

Avatar of mrwad99
mrwad99Flag for United Kingdom of Great Britain and Northern Ireland asked on
24 Comments1 Solution443 ViewsLast Modified:
Ah hello.

I have come across a serious problem regarding a singleton class I have in a DLL.  To put it short, I am finding that I get two instances of the same singleton created only lines apart in my code.  This is baffling.

Please see the class diagram VS has created for at

I have a test app that calls the following:

BOOL CMyTestTestDlg::OnInitDialog()

      MyNamespace::CSessionManager<MyNamespace::CEngine1>& instance = MyNamespace::CSessionManager<MyNamespace::CEngine1>::Instance();

      instance.RunFunc ();

      return TRUE;

Now, this results in RunFunc() being called:

BOOL RunFunc ()
      CSessionManager<CEngine1>& instance = CSessionManager<CEngine1>::Instance();
      return ExecuteSynchronousTask ( );

You can see I obtain a second instance at this point, just to make sure it is the same one as was initialised in my dialog test app.  It is, since its m_bInitialised member is TRUE.  

We then get

BOOL CBaseEngine::ExecuteSynchronousTask ()
      CSessionManager<CEngine1>& i2 = CSessionManager<CEngine1>::Instance();

      return TRUE;

This is where things go wrong.  i2 insists on creating a second object, not using the one that has already been created.  This is causing major inconsistency (in reality, the objects are far more complicated than is shown).

Someone please tell me what is going on here and how I can correct it.  It is crucial that I can access the same object that was initialised in my test app from within my DLL.  It cannot be a DLL-boundary issue since the call to MyFunc() is executed within the DLL, and that works fine.

I have created a very simple project (<100 lines) to demonstrate this problem; please download it at (vS 2008 format).

Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 24 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 24 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros