Boost logo

Boost Users :

Subject: [Boost-users] [serialization] can't serialize to derived archive via base class ptr
From: frankg (frank.gellert_at_[hidden])
Date: 2011-09-02 09:02:15


Hi,

I try to serialize a derived object by its base class pointer to an archive
I derived from xml_oarchive_impl exactly the same way as it is documented in
the example code here:

http://www.boost.org/doc/libs/1_47_0/libs/serialization/example/log_archive.cpp
http://www.boost.org/doc/libs/1_47_0/libs/serialization/example/log_archive.hpp

However, when I start the program (to output to my derived archive) I get
the assertion (“BOOST_ASSERT(false)”) in the following method in
“basic_serializer_map.cpp” (I checked with the debugger (VS2010) and found
the m_map.size() is 0).

BOOST_ARCHIVE_DECL(const basic_serializer *)
basic_serializer_map::find(
    const boost::serialization::extended_type_info & eti
) const {
    const basic_serializer_arg bs(eti);
    map_type::const_iterator it;
    it = m_map.find(& bs);
    if(it == m_map.end()){
        BOOST_ASSERT(false);
        return 0;
    }
    return *it;
}

If I use the DERIVED class pointer it works fine with my own derived
archive. If I use a BASE class pointer and use the ORIGINAL xml_oarchive it
works fine too. Only when I combine both (using my own derived archive AND
try to serialize through a base class pointer) it fails with the assertion.

Here is an excerpt of my code and 3 cases, where the first and second work
fine and the third asserts:

THIS WORKS FINE:

int _tmain(int argc, _TCHAR* argv[])
{
    Base* bptr = new Derived; // using base class ptr

    boost::archive::xml_oarchive oa(std::cout, boost::archive::no_header);
// using original archive from boost::serialization

    oa & BOOST_SERIALIZATION_NVP(bptr);

    return 0;
}

THIS WORKS FINE TOO:

int _tmain(int argc, _TCHAR* argv[])
{
    Derived* dptr = new Derived; // using derived class ptr

    log_archive oa(std::cout, boost::archive::no_header); // using derived
archive

    oa & BOOST_SERIALIZATION_NVP(dptr);

    return 0;
}

THIS ASSERTS:

int _tmain(int argc, _TCHAR* argv[])
{
    Base* bptr = new Derived; // using base class ptr

    log_archive oa(std::cout, boost::archive::no_header); // using derived
archive

    oa & BOOST_SERIALIZATION_NVP(bptr);

    return 0;
}

Is there something I can do to get my derived archive correctly working or
doesn’t support the boost::serialization lib the combination of deriving
archives and serializing via base class pointers at the same time?

One more hint: using the debugger I saw that the m_map member in
"basic_serializer_map.hpp" is statically initialized before main() is
started. Some information for all my derived classes which contain
serialization-methods where inserted into m_map. So m_map isn't actually
empty but it seems to be a different object than the one where m_map has
been initialized.

Cheers,
Frank

--
View this message in context: http://boost.2283326.n4.nabble.com/serialization-can-t-serialize-to-derived-archive-via-base-class-ptr-tp3786005p3786005.html
Sent from the Boost - Users mailing list archive at Nabble.com.

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