Destruction-Managed SingletonA Compound Pattern for Reliable Deallocation of Singletons

Destruction-Managed Singleton
A Compound Pattern for Reliable Deallocation of Singletons
Evgeniy Gabrilovich

Listing 3. Sample resource definition (logger.h).

#include <memory>
#include <string>
#include <stdexcept>
#include "dphase.h"
#include "destructor.h"
using namespace std;

class Logger {
  typedef auto_ptr<Logger> LoggerPtr;

  static LoggerPtr& get_instance();
  static void destroy_instance() { delete get_instance().release(); }


  friend class auto_ptr<Logger>;
  friend class TDestructor<Logger>;

  // checked singleton interface
  static Logger& instance() throw (std::logic_error) { 
     Logger* logger = get_instance().get();
     if ( !logger )
       throw logic_error("Logger is not available!");
     return *logger;
  void log(string message);

About the Author

Evgeniy Gabrilovich is a Strategic Development Team Leader at Comverse Technology Inc. He holds an M.Sc. in Computer Science from the Technion-Israel Institute of Technology. He may be contacted at [email protected].