views:

111

answers:

1

What is the best way of initializing and terminating an application?

  1. The library needs to be initialized/terminated only once and can be used by any number of dlls.
  2. Is there any standard design to accomplish this?
  3. This initialization has to be the very first step.

Is singleton is what I need here. Any number of dlls which are loaded will use the same instance or a separate one?

I am using a log4cxx logger implementation. I want this log4cxx should be initialized only once. Can anybody point to a pseudocode

+2  A: 

Most applications have a single entry point (for standard C++ it's called main) where you can create objects that last the lifetime of the application.

From your description it sounds like you are actually writing a service library which is used in other applications and not the application (in the conventional sense) itself.

The only safe thing to do is to make your service re-initializable as, there is nothing that can stop an application from dynamically loading a dll that links to your library, explicitly unloading that dll and then re-loading it again.

To your library, there is no observable difference between the intermediate dll unload and normal termination.

While your library is loaded, you can use shared/global data to track initializations so that you only perform expensive initialization steps that can be shared between clients once.

Perhaps you should outline what the difficulty with having your library re-initialized would cause?

Charles Bailey