
Can someone please explain why this code: struct Foo : boost::enable_shared_from_this<Foo> {}; Foo* p_foo = new Foo; boost::shared_ptr<Foo> ptr_foo(p_foo->shared_from_this()); is throwing? msvcr100d.dll!_CxxThrowException(void * pExceptionObject, const _s__ThrowInfo * pThrowInfo) Line 157 C++ boost::throw_exceptionboost::bad_weak_ptr(const boost::bad_weak_ptr & e) Line 61 + 0x4d bytes C++ boost::detail::shared_count::shared_count(const boost::detail::weak_count & r) Line 421 + 0x23 bytes C++ boost::shared_ptr<`anonymous namespace'::Foo>::shared_ptr<`anonymous namespace'::Foo><`anonymous namespace'::Foo>(const boost::weak_ptr<`anonymous namespace'::Foo> & r) Line 216 C++ boost::enable_shared_from_this<`anonymous namespace'::Foo>::shared_from_this() Line 49 + 0xf bytes C++
`anonymous namespace'::test_method() Line 41 + 0xf bytes C++
Since Foo embeds a weak_ptr, why can't it new the pi_ on the spot? (I'm using Boost 1.44). Thanks, --DD PS: On a related note, why doesn't boost::shared_ptr<Foo>(p_foo) (where p_foo is a raw Foo*) implicitly apply the aliasing Ctor, to share the same px, to avoid the classic bug of having several different pn on the same px, since p_foo derives from enable_shared_from_this()? Initially i thought that's what sp_enable_shared_from_this() and pe->_internal_accept_owner() were meant to do, but obviously that's not the case.