0

Possible Duplicate:
enable_shared_from_this - empty internal weak pointer?

AuthConnection::AuthConnection(boost::asio::io_service& io_service)
    :Connection(io_service)
{
    boost::shared_ptr<AuthHandler>h (new AuthHandler( shared_from_this() ));
    this->handler=h;
}

shared_from_this() should return a ptr of Connection , but it's throwing this exception

tr1::bad_weak_ptr

i dont have a clue what's wrong here!

Community
  • 1
  • 1
Abanoub
  • 3,623
  • 16
  • 66
  • 104

1 Answers1

3

I'm guessing your usage of shared_from_this() is wrong.

It should be used in a class as follows:

class Y: public enable_shared_from_this<Y>
{
public:

    shared_ptr<Y> f()
    {
        return shared_from_this();
    }
}

Then you can call y->f() to get the this pointer of the class.

It's more then merely this, the actual issue is explained in this question and answer. It has to do with the fact that you can't call shared_from_this() in the ctor of the derived object.

Community
  • 1
  • 1
Tony The Lion
  • 61,704
  • 67
  • 242
  • 415
  • 1
    i tested it and the same error,notice that am driving from it so i dont really need a method to get the ptr – Abanoub Jun 09 '11 at 07:22
  • @Mixed I added something to my answer. – Tony The Lion Jun 09 '11 at 07:26
  • so i made `boost::shared_ptr con=this->getThis(); boost::shared_ptrh (new AuthHandler(con));` and still the same error , or did i get it wrong!? – Abanoub Jun 09 '11 at 08:30
  • 4
    @MixedCoder We don't know what the relationships between `Connection`, `AuthConnection` and `AuthHandler` are; we don't know what *it* is in "*notice that am driving from it*" (sic) nor do we even know **where** we're supposed to notice it. By this point you should expect an answer from the Psychic Squad. – Luc Danton Jun 09 '11 at 09:02