Reputation: 487
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
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
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