   pool objects outliving pool
  ive got a pool which returns shared_ptr's to objects... i create the shard_ptr's so that they call the pools deallocate/return function... but if the pool outlives the pool than that call will crash the program... this shouldnt never happen but if it does i cant tolerate my application crashing...i try to solve it the following way but i dont think its very good...class Factory{  public:  shared_ptr<SomeType> Create()  {    return shared_ptr<SomeType>(pool.allocate(), boost::bind(&Factory::Return, this, _1));  }  static void Return(Factory* pFactory, SomeType* pSomeType)  {    try    {  pFactory->pool.deallocate(pSomeType); // catch exception    }    catch(...)    {}    delete pSomeType;  }private:   SomePool pool;};is there any way i can guarantee that the pool will always outlive its allocated objects? or some other way i can improve this?   
  I guess I would simply wrap the pool in a class that keeps a shared_ptr to the pool, and then pass that shared_ptr<Pool> on to the deleter in the returned shared_ptr<SomeType>. Then the pool won't be deleted until all shared_ptrs have been deleted. Assuming a simple pool interface like,template<class T>struct SimplePool{  typedef T elem_t;  T* alloc(){ return new T; }};It would look something like (not tested, but the idea should work):template<class Pool>class PoolElemDeleter{  std::shared_ptr<Pool> pool_;public:  typedef typename Pool::elem_t elem_t;  void operator()(elem_t* ptr)  {    delete ptr;  }};template<class Pool>class PoolWrapper{  std::shared_ptr<Pool> pool_;public:  typedef typename Pool::elem_t elem_t;  PoolWrapper()    :    pool_(new Pool)  {}  std::shared_ptr<elem_t> alloc()  {    return std::shared_ptr<elem_t>(pool_->alloc(), PoolElemDeleter<Pool>(pool_) );  }};