boost :: serialization反序列化xml_archive异常

时间:2010-06-27 11:46:38

标签: c++ serialization boost

我试图将一些对象序列化为xml然后再读回来 代码是: IncomingTradeMessage继承BaseMessage并包含InternalRequestInfo,而InternalRequestInfo又包含InternalTradeTransInfo。这是代码:

std::ostringstream oStringStream;
boost::archive::xml_oarchive xmlArchive(oStringStream);

xmlArchive.register_type(static_cast<BaseMessage *>(NULL));
xmlArchive.register_type(static_cast<IncomingTradeMessage *>(NULL));
xmlArchive.register_type(static_cast<InternalRequestInfo *>(NULL));
xmlArchive.register_type(static_cast<InternalTradeTransInfo *>(NULL));


const BaseMessage* myMessage =message;

xmlArchive << make_nvp("Message", myMessage);

std::string strData = oStringStream.str();

std::istringstream iStringStream(strData,istringstream::in);
std::string nothing;

//iStringStream. >> nothing;

BaseMessage* x = new IncomingTradeMessage();

boost::archive::xml_iarchive xmlIncomingArchive(iStringStream);

xmlIncomingArchive >> make_nvp("Message",*x);

当尝试反序列化基础的第一个成员时,我得到: mtsrv.exe中0x7c812aeb处的未处理异常:Microsoft C ++异常:内存位置0x0eb6df58处的boost :: archive :: archive_exception ..

在:

  kernel32.dll!7c812aeb()  
  [Frames below may be incorrect and/or missing, no symbols loaded for kernel32.dll] 
  kernel32.dll!7c812aeb()  
  xxxServerAPI.dll!_CxxThrowException(void * pExceptionObject=0x0eb6df58, const _s__ThrowInfo * pThrowInfo=0x05481c68)  Line 161 C++
> xxxServerAPI.dll!boost::serialization::throw_exception<boost::archive::archive_exception>(const boost::archive::archive_exception & e={...})  Line 37 C++
  xxxServerAPI.dll!boost::archive::basic_xml_iarchive<boost::archive::xml_iarchive>::load_end(const char * name=0x05458c20)  Line 53 + 0x35 bytes C++
  xxxServerAPI.dll!boost::archive::basic_xml_iarchive<boost::archive::xml_iarchive>::load_override<std::basic_string<char,std::char_traits<char>,std::allocator<char> > >(const boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > & t={...}, int __formal=0)  Line 88 C++
  xxxServerAPI.dll!boost::archive::xml_iarchive_impl<boost::archive::xml_iarchive>::load_override<boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > const >(const boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > & t={...}, int __formal=0)  Line 81 C++
  xxxServerAPI.dll!boost::archive::detail::interface_iarchive<boost::archive::xml_iarchive>::operator>><boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > const >(const boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > & t={...})  Line 62 C++
  xxxServerAPI.dll!boost::archive::detail::interface_iarchive<boost::archive::xml_iarchive>::operator&<boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > const >(const boost::serialization::nvp<std::basic_string<char,std::char_traits<char>,std::allocator<char> > > & t={...})  Line 69 C++
  xxxServerAPI.dll!x::Native::xServerApi::Messages::BaseMessage::serialize<boost::archive::xml_iarchive>(boost::archive::xml_iarchive & ar={...}, const unsigned int version=0)  Line 30 C++
  xxxServerAPI.dll!boost::serialization::access::serialize<boost::archive::xml_iarchive,x::Native::xServerApi::Messages::BaseMessage>(boost::archive::xml_iarchive & ar={...}, x::Native::xServerApi::Messages::BaseMessage & t={...}, const unsigned int file_version=0)  Line 119 C++
  xxxServerAPI.dll!boost::serialization::serialize<boost::archive::xml_iarchive,x::Native::xServerApi::Messages::BaseMessage>(boost::archive::xml_iarchive & ar={...}, x::Native::xServerApi::Messages::BaseMessage & t={...}, const unsigned int file_version=0)  Line 74 + 0x11 bytes C++
  xxxServerAPI.dll!boost::serialization::serialize_adl<boost::archive::xml_iarchive,x::Native::xServerApi::Messages::BaseMessage>(boost::archive::xml_iarchive & ar={...}, x::Native::xServerApi::Messages::BaseMessage & t={...}, const unsigned int file_version=0)  Line 133 + 0x18 bytes C++
  xxxServerAPI.dll!boost::archive::detail::iserializer<boost::archive::xml_iarchive,x::Native::xServerApi::Messages::BaseMessage>::load_object_data(boost::archive::detail::basic_iarchive & ar={...}, void * x=0x054e1490, const unsigned int file_version=0)  Line 186 + 0x1a bytes C++
  xxxServerAPI.dll!boost::archive::detail::basic_iarchive_impl::load_object(boost::archive::detail::basic_iarchive & ar={...}, void * t=0x054e1490, const boost::archive::detail::basic_iserializer & bis={...})  Line 400 C++
  xxxServerAPI.dll!boost::archive::detail::basic_iarchive::load_object(void * t=0x054e1490, const boost::archive::detail::basic_iserializer & bis={...})  Line 548 C++
  xxxServerAPI.dll!boost::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive>::load_standard::invoke<x::Native::xServerApi::Messages::BaseMessage>(boost::archive::xml_iarchive & ar={...}, const x::Native::xServerApi::Messages::BaseMessage & t={...})  Line 387 C++
  xxxServerAPI.dll!boost::archive::detail::load_non_pointer_type<boost::archive::xml_iarchive>::invoke<x::Native::xServerApi::Messages::BaseMessage>(boost::archive::xml_iarchive & ar={...}, x::Native::xServerApi::Messages::BaseMessage & t={...})  Line 433 + 0xd bytes C++
  xxxServerAPI.dll!boost::archive::load<boost::archive::xml_iarchive,x::Native::xServerApi::Messages::BaseMessage>(boost::archive::xml_iarchive & ar={...}, x::Native::xServerApi::Messages::BaseMessage & t={...})  Line 580 + 0xd bytes C++
  xxxServerAPI.dll!boost::archive::detail::common_iarchive<boost::archive::xml_iarchive>::load_override<x::Native::xServerApi::Messages::BaseMessage>(x::Native::xServerApi::Messages::BaseMessage & t={...}, int __formal=0)  Line 68 + 0x15 bytes C++
  xxxServerAPI.dll!boost::archive::basic_xml_iarchive<boost::archive::xml_iarchive>::load_override<x::Native::xServerApi::Messages::BaseMessage>(const boost::serialization::nvp<x::Native::xServerApi::Messages::BaseMessage> & t={...}, int __formal=0)  Line 87 C++
  xxxServerAPI.dll!boost::archive::xml_iarchive_impl<boost::archive::xml_iarchive>::load_override<boost::serialization::nvp<x::Native::xServerApi::Messages::BaseMessage> const >(const boost::serialization::nvp<x::Native::xServerApi::Messages::BaseMessage> & t={...}, int __formal=0)  Line 81 C++
  xxxServerAPI.dll!boost::archive::detail::interface_iarchive<boost::archive::xml_iarchive>::operator>><boost::serialization::nvp<x::Native::xServerApi::Messages::BaseMessage> const >(const boost::serialization::nvp<x::Native::xServerApi::Messages::BaseMessage> & t={...})  Line 62 C++
  xxxServerAPI.dll!x::Native::xServerApi::Bridge::DistributeMessage(const x::Native::xServerApi::Messages::BaseMessage * message=0x04799248)  Line 99 C++

2 个答案:

答案 0 :(得分:1)

看来你正在序列化一个指向基类的指针:

const BaseMessage* myMessage =message;
xmlArchive << make_nvp("Message", myMessage);

但反序列化对基类的引用:

BaseMessage* x = new IncomingTradeMessage();
xmlIncomingArchive >> make_nvp("Message",*x);

如果您将*x更改为x会怎样?

答案 1 :(得分:0)

这很好用:

std::ostringstream oStringStream;
boost::archive::xml_oarchive xmlArchive(oStringStream);

xmlArchive.register_type(static_cast<BaseMessage *>(NULL));
xmlArchive.register_type(static_cast<IncomingTradeMessage *>(NULL));
xmlArchive.register_type(static_cast<InternalRequestInfo *>(NULL));
xmlArchive.register_type(static_cast<InternalTradeTransInfo *>(NULL));


const BaseMessage* myMessage =message;

xmlArchive << make_nvp("Message", myMessage);

std::string strData = oStringStream.str();

std::istringstream iStringStream(strData,istringstream::in);
std::string nothing;

//iStringStream. >> nothing;

BaseMessage* x;

boost::archive::xml_iarchive xmlIncomingArchive(iStringStream);


xmlIncomingArchive.register_type(static_cast<BaseMessage *>(NULL));
xmlIncomingArchive.register_type(static_cast<IncomingTradeMessage *>(NULL));
xmlIncomingArchive.register_type(static_cast<InternalRequestInfo *>(NULL));
xmlIncomingArchive.register_type(static_cast<InternalTradeTransInfo *>(NULL));

xmlIncomingArchive >> make_nvp("Message",*x);