Arne Böckmann
Arne Böckmann

Reputation: 487

Boost 1.65.1 serialization to vector fails with 'Assertion initialized_ failed'

I have the following test case that serializes an int into std:vector.

It crashes with the following assertion:

serialization_test: /usr/include/boost/iostreams/detail/optional.hpp:55: T& boost::iostreams::detail::optional<T>::operator*() [with T = boost::iostreams::detail::concept_adapter<boost::iostreams::back_insert_device<std::vector<char> > >]: Assertion `initialized_' failed.

Any ideas? I think this should work...

BOOST_AUTO_TEST_CASE(serialize_base_test)
{
    int t = 42;
    std::vector<char> buffer;

    iostreams::back_insert_device<std::vector<char>> sink{buffer};
    iostreams::stream<iostreams::back_insert_device<std::vector<char>>> os;
    os.open(sink);
    archive::binary_oarchive oa(os);

    oa << t;
    os.flush();
    os.close();

}

edit: I simplified the test case.

Upvotes: 1

Views: 193

Answers (2)

Arne B&#246;ckmann
Arne B&#246;ckmann

Reputation: 487

Ok I figured it out. The binary_oarchive does something inside its destructor that requires that the stream is still open. The following works:

BOOST_AUTO_TEST_CASE(serialize_base_test)
{
    int t = 42;
    std::vector<char> buffer;

    iostreams::back_insert_device<std::vector<char>> sink{buffer};
    iostreams::stream<iostreams::back_insert_device<std::vector<char>>> os;
    os.open(sink);
    {
        archive::binary_oarchive oa(os);
        oa << t; 
    }

}

Upvotes: 2

user7860670
user7860670

Reputation: 37468

Destructor for an archive should be called before the stream is closed because it restores any altered stream facets to their state before the archive was opened.

Upvotes: 3

Related Questions