Boost logo

Boost :

Subject: [boost] shared_from_this() fails from boost.serialization load()
From: Allan Johns (allan.johns_at_[hidden])
Date: 2011-02-18 01:28:21


Hi,

Consider class A, which contains an instance of class B, which in turn
contains a weak reference to its parent A instance (note: pseudocode):

class A;

class B {
    weak_ptr<A> m_parent;
}

class A {
  shared_ptr<B> m_child;
}

I have a case where I am boost-serializing class A, and its load() method
looks like so:

template<class Archive>
void A::load(Archive& ar, const unsigned int)
{
  ar & m_child;
  m_child->m_parent = shared_from_this();
}

Note that B does *not* serialize its m_parent member (this is by design).

The problem I have is that the shared_from_this() call in A::load() fails,
even though I am guaranteeing that A is serialized via a shared_ptr.
Presumably boost.serialize is not placing the default-constructed instance
of A into its containing shared_ptr<A>, before calling A::load().

My question is, is there any reason why boost.serialize couldn't deal with
this special case, which would allow me to call shared_from_this() from
load()?

I don't have a workaround for this in my code, other than to resort to a raw
ptr for B::m_parent.

ps - I suppose you could argue that since you can't do this in A's
constructor, you shouldn't be able to do it in its load() function either.
However I do have a legitimate case where I need to do this... I can
extrapolate on the whole situation if necessary, but I've left it out here
in the interests of simplicity).

Thanks boosters,
Allan


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk