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!

link|flag

57% accept rate
1  

1 Answer

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.

link|flag
1  
@Tony The Tiger i tested it and the same error,notice that am driving from it so i dont really need a method to get the ptr – MixedCoder 2 hours ago
@Mixed I added something to my answer. – Tony The Tiger 2 hours ago
@Tony The Tiger so i made boost::shared_ptr<Connection> con=this->getThis(); boost::shared_ptr<AuthHandler>h (new AuthHandler(con)); and still the same error , or did i get it wrong!? – MixedCoder 1 hour ago
2  
@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 42 mins ago

Your Answer

 
or
required, but never shown

Not the answer you're looking for? Browse other questions tagged or ask your own question.