Destruction-Managed SingletonA Compound Pattern for Reliable Deallocation of Singletons

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

Listing 2. Destruction Manager (dmanager.cxx).


#include <algorithm>  // for sort()
#include "dmanager.h"
#include "destructor.h"
using namespace std;

DestructionManager::~DestructionManager() {
  for (int i = 0; i < m_destructors.size();="" ++i)="" delete="" m_destructors[i];="" }="" template="" <class="" t>="" class="" greater_ptr="" {="" public:="" typedef="" t*="" t_ptr;="" bool="" operator()(const="" t_ptr&="" lhs,="" const="" t_ptr&="" rhs)="" const="" {="" return="" *lhs=""> *rhs; }
};

void DestructionManager::destroy_objects() {
  // sort the destructors in decreasing order
  sort(m_destructors.begin(), m_destructors.end(),
         greater_ptr<Destructor>());

  // destroy the objects
  for (int i = 0; i < m_destructors.size();="" ++i)="" m_destructors[i]-="">destroy();
}

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].