Boost序列化的"input stream error"问题
最近在使用boost序列化进行反序列化时,遇到如下抛错:
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error-Success
Aborted (core dumped)
源代码片段如下:
using OA = boost::archive::xml_oarchive; using IA = boost::archive::xml_iarchive; std::string content; std::ostringstream os; OA oa(os); oa << BOOST_SERIALIZATION_NVP(info); content = os.str(); std::cout << os.str() << std::endl; std::istringstream is(content); IA ia(is); ia >> BOOST_SERIALIZATION_NVP(info_new);
我有分别使用boost 1.59跟boost 1.68两个版本,同样的代码在boost 1.59没问题,但在boost 1.68就抛出异常。
后来在网上查到一个帖子:https://blog.csdn.net/o_longzhong/article/details/80090379
简单说就是要让archive对象先正常析构,参照修改,问题得到解决,修改后代码段如下:
std::string content; std::ostringstream os; { OA oa(os); oa << BOOST_SERIALIZATION_NVP(info); } content = os.str(); std::cout << os.str() << std::endl; std::istringstream is(content); { IA ia(is); ia >> BOOST_SERIALIZATION_NVP(info_new); }
除了上面这种情况,还发现有种情况也是会引起这个异常:
用对象去进行序列化,但是用指针去反序列化。
别问我为什么要这么去操作,总之这问题困了我一两天,唉,自作自受吧!

浙公网安备 33010602011771号