Boost logo

Boost Users :

Subject: [Boost-users] Problem with enable_shared_from_this and shared_ptr
From: Ryan McConnehey (mccorywork_at_[hidden])
Date: 2010-07-20 22:06:55


If class "Pool" inherits from enable_shared_from_this the documentation
says I'm able to do the following.

shared_ptr<Pool> aPool = shared_from_this();

The minimal code I've included shows that this isn't happening. The
code compiles but I get this error "tr1::bad_weak_ptr". What should I
be doing different?

Ryan

[CODE]

//C++ Includes
#include <exception>
#include <iostream>
#include <vector>
#include <algorithm>

//Boost Includes
#include <boost/shared_ptr.hpp>
#include <boost/enable_shared_from_this.hpp>

class pool : public boost::enable_shared_from_this< pool > {
private:
  using boost::enable_shared_from_this< pool >::shared_from_this;

public:
  boost::shared_ptr<int> get(void) {
    boost::shared_ptr<pool> aPool = shared_from_this(); //This is
where the tr1::bad_weak_ptr happens.
    return boost::shared_ptr<int>(new int);
  }
};

int main(int argc, char * argv[]) {
  try {
    pool * myPool = new pool;

    boost::shared_ptr<int> a = myPool->get();
  }
  catch (std::exception & exception) {
    std::cout << exception.what() << std::endl;
  }

  return 0;
}


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net