Proper initialization of static objects (singletons) with splash screen support | CodeGuru

Proper initialization of static objects (singletons) with splash screen support

Environment: Win98, VC 5.0 Introduction: Do you have code like this in your InitInstance() ? … ClassXY::GlobalInit(); ClassZA::GlobalSetup(); ClassMurx::Prepare(); … Then this is what you need! The cdxCInitializerT base class helps you to design a proper, clear interface to initialize any objects you want in application scope (i.e. your CWinApp object exists during the initialization).  […]

Written By
CodeGuru Staff
CodeGuru Staff
Mar 2, 1999
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: Win98, VC 5.0

Introduction:

Do you have code like this in your InitInstance() ?

  …
  ClassXY::GlobalInit();
  ClassZA::GlobalSetup();
  ClassMurx::Prepare();
  …

Then this is what you need!

The cdxCInitializerT base class helps you to design
a proper, clear interface to initialize any objects you want in application scope (i.e. your
CWinApp object exists during the initialization). 
The basic idea is to design a class (cdxCInitializerT)
that has two static members, InitInstance() and
ExitInstance() which must be called from within your
application’s InitInstance()/ExitInstance().

These functions should now initialize all exisiting objects of
cdxCInitializerT by calling a virtual function
OnInitInstance().  Conversely,
OnExitInstance() is called to clean up what you
allocated using OnInitInstance().  This is exactly
what the cdxCInitializerT class offers you  – 
an interface to initialize/clean up your objects that have been constructed in global scope
(static members variables, global variables).

Moreover, the class can be parameterized (by two template arguments) to give you the chance
to provide feedback to the calling function (i.e. each of your objects can get a pointer to
a "splash screen" dialog where it can write nice stuff into).  Additionally,
the design gives you the opportunity to define an ordering of the initialization (object A
will be initialized after B for sure).

Finally, a former class, cdxCInitializerThreadT has been
added that helps you to implement a splash screen in a second thread easily (you just need a
dialog and some member functions to modify its controls – that’s it!).

If you use my class, you should make your InitInstance()
look like this:

  …
  cdxCInitializerThreadT<CSplashScreen> splash;
  if (!cdxCInitializer<..>::InitInstance(splash))
     return FALSE;

and all your nasty classes that used a "GlobalInit()" before will be set up properly –
and adding new "GlobalInit()" classes will be easy;  you don’t need to modify your
application’s implementation file anymore.

Features:

  • Support for ordered initialization and clean-up.
  • Support for "Feedback" objects (splash
    screens).
  • Support to quickly implement a splash screen with a
    second thread.
  • Rather complete documentation with tutorial and function
    reference.

Download:

Source code and documentation   Initializer_src.zip (17 KB)

Sample project (includes source code and documentation)   Initializer_proj.zip (61 KB)

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.