enable_shared_from_this and constructor

Hello, why is not possible to call this: -- cut here -- #include <boost/enable_shared_from_this.hpp> class Trida; boost::shared_ptr<Trida> trida; class Trida : public boost::enable_shared_from_this<Trida> { public: Trida() { trida = this->shared_from_this(); }; }; int main() { boost::shared_ptr<Trida> tr(new Trida()); return 0; } -- cut here -- It throws in constructor of shared_count(weak_count const & r). I think the problem is that there is no one shared_ptr instance that owns object tr yet when calling constructor. May be it is BUG (and I don't know how to handle that). Can be at least note in documentation? -- Petr "Fers" Ferschmann -=[ petr@ferschmann.cz ]==[ http://petr.ferschmann.cz/ ]=- -=[ Koukni na http://www.postcard.cz/ ]==[ +420 604/781 009 ]=- GPG Fingerprint: [83B0 6378 7A9D D993 035E 60BD FEEC F665 D2C8 1B9A] [Non-text portions of this message have been removed]

Petr Ferschmann wrote:
Hello,
why is not possible to call this: [...] class Trida : public boost::enable_shared_from_this<Trida> { public: Trida() { trida = this->shared_from_this(); }; }; [...] It throws in constructor of shared_count(weak_count const & r).
I think the problem is that there is no one shared_ptr instance that owns object tr yet when calling constructor.
You are right. To call shared_from_this, you need first to ensure that at least one shared_ptr points to the object, as stated in the Requires clause.
May be it is BUG (and I don't know how to handle that). Can be at least note in documentation?
See http://www.boost.org/libs/smart_ptr/sp_techniques.html#in_constructor
participants (2)
-
Peter Dimov
-
Petr Ferschmann