make new SetLongevity impl more exception safety

git-svn-id: svn://svn.code.sf.net/p/loki-lib/code/trunk@359 7ec92016-0320-0410-acc4-a06ded1c099a
This commit is contained in:
syntheticpp 2005-11-13 14:32:03 +00:00
parent f10f61534c
commit a1b035fd15

View file

@ -24,6 +24,7 @@
#include <new> #include <new>
#include <vector> #include <vector>
#include <list> #include <list>
#include <memory>
#ifdef _MSC_VER #ifdef _MSC_VER
@ -138,18 +139,22 @@ namespace Loki
if(pTrackerArray==0) if(pTrackerArray==0)
pTrackerArray = new TrackerArray; pTrackerArray = new TrackerArray;
LifetimeTracker* p = new ConcreteLifetimeTracker<T, Destroyer>( // automatically delete the ConcreteLifetimeTracker object when a exception is thrown
pDynObject, longevity, d); std::auto_ptr<LifetimeTracker>
p( new ConcreteLifetimeTracker<T, Destroyer>(pDynObject, longevity, d) );
// Find correct position // Find correct position
TrackerArray::iterator pos = std::upper_bound( TrackerArray::iterator pos = std::upper_bound(
pTrackerArray->begin(), pTrackerArray->begin(),
pTrackerArray->end(), pTrackerArray->end(),
p, p.get(),
LifetimeTracker::Compare); LifetimeTracker::Compare);
// Insert a pointer to the object into the queue // Insert the pointer to the ConcreteLifetimeTracker object into the queue
pTrackerArray->insert(pos, p); pTrackerArray->insert(pos, p.get());
// nothing has thrown: don't delete the ConcreteLifetimeTracker object
p.release();
// Register a call to AtExitFn // Register a call to AtExitFn
std::atexit(Private::AtExitFn); std::atexit(Private::AtExitFn);