[SmartPtr] enable_shared_from_raw / weak_from_raw remains not associated

I'm converting existing code with parent-child object tree to boost
shared_ptr/weak_ptr with parents having [now shared] ownership over the
children and children having weak reference to the parent. In the existing
code a parent usually becomes the children as constructor parameters, and
thus I have to extract the weak_ptr to "this" in constructor, which does
not work with current boost::enable_shared_from_this implementation. So I
am looking to enable_shared_from_raw alternative, which is promising, but
unfortunately does not quite work for my case. Here is a small example
demonstrating the issue:
test.cpp:
#include

On Thu, May 16, 2013 at 5:58 AM, Slava
I'm converting existing code with parent-child object tree to boost shared_ptr/weak_ptr with parents having [now shared] ownership over the children and children having weak reference to the parent. In the existing code a parent usually becomes the children as constructor parameters, and thus I have to extract the weak_ptr to "this" in constructor, which does not work with current boost::enable_shared_from_this implementation. So I am looking to enable_shared_from_raw alternative, which is promising, but unfortunately does not quite work for my case. Here is a small example demonstrating the issue:
struct Parent : public boost::enable_shared_from_raw { Parent(boost::shared_ptr<Child> _child) { std::cout << "Construct Parent" << std::endl; this->child = _child; this->child->parent = boost::weak_from_raw(this); }
weak_from_raw() doesn't work until after a shared_ptr has taken ownership of the object or shared_from_raw() has been called. It's been long enough don't remember what the rationale for this was, or if there even was a rationale, but the enable_shared_from_raw test case seems to expect this behavior. From looking at the code, it seems like if you just called shared_from_raw prior to calling weak_from_raw it would work.

On Thu, 16 May 2013 20:09:58 +0200, Frank Mori Hess
On Thu, May 16, 2013 at 5:58 AM, Slava
wrote: I'm converting existing code with parent-child object tree to boost shared_ptr/weak_ptr with parents having [now shared] ownership over the children and children having weak reference to the parent. In the existing code a parent usually becomes the children as constructor parameters, and thus I have to extract the weak_ptr to "this" in constructor, which does not work with current boost::enable_shared_from_this implementation. So I am looking to enable_shared_from_raw alternative, which is promising, but unfortunately does not quite work for my case. Here is a small example demonstrating the issue:
struct Parent : public boost::enable_shared_from_raw { Parent(boost::shared_ptr<Child> _child) { std::cout << "Construct Parent" << std::endl; this->child = _child; this->child->parent = boost::weak_from_raw(this); }
weak_from_raw() doesn't work until after a shared_ptr has taken ownership of the object or shared_from_raw() has been called. It's been long enough don't remember what the rationale for this was, or if there even was a rationale, but the enable_shared_from_raw test case seems to expect this behavior. From looking at the code, it seems like if you just called shared_from_raw prior to calling weak_from_raw it would work.
Thanks, right, the test cases, I always forget they are the good source of
information!
The example works as expected (difference to previous is only addition of
boost::shared_from_raw(this) in constructor):
#include

On Fri, May 17, 2013 at 3:22 AM, Slava
boost::shared_from_raw(this); // necessary for weak_from_raw() to function properly this->child->parent = boost::weak_from_raw(this);
I reached what I needed, but I have some bad feeling about how it works. It does not look sane, boost::shared_from_raw(this) class is there only to produce the desired side effect.
You could just not use weak_from_raw at all. You can initialize your weak_ptr from the shared_ptr returned by shared_from_raw. -- Frank
participants (2)
-
Frank Mori Hess
-
Slava