Boost logo

Boost Users :

Subject: Re: [Boost-users] Serialization of optional<std::string>
From: Chardrazle (boost_at_[hidden])
Date: 2018-02-21 13:06:52


>I would recommend tracing with the debugger trough serialization of
>optional<string> in portable binary archive to see how it's different
>from serialization in binary or text archive.

I've performed a bit more analysis and it looks like serialization of
optional<> with any non-trivial types is broken in 1.64.

The serialization/optional.hpp load function:

template<class Archive, class T>
void load(
    Archive & ar,
    boost::optional< T > & t,
    const unsigned int /*version*/
)[...]

In 1.63, constructed space for the type with:

        detail::stack_construct<Archive, T> aux(ar, item_version);
        ar >> boost::serialization::make_nvp("value", aux.reference());
        t.reset(aux.reference());

which ensures the constructor is called. However, for 1.64 this changed to:

    detail::stack_allocate<T> tp; // <<<<<<<<<<<<<<
    ar >> boost::serialization::make_nvp("value", tp.reference());
    t.reset(boost::move(tp.reference()));
    ar.reset_object_address(
        t.get_ptr(),
        & tp.reference()
    );

Unfortunately, this only allocates (zero'd) space for the object, and some
objects (in this case, std::string in g++ 4.6) are not valid as such.

I see that in 1.65 this changed again, to the simpler:

    if(! t.is_initialized())
        t = T();
    ar >> boost::serialization::make_nvp("value", *t);

It seems I was just unlucky to choose 1.64. I've moved to 1.66 and it's all
working.

--
Sent from: http://boost.2283326.n4.nabble.com/Boost-Users-f2553780.html

Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net