Boost C ++ xml_oarchive运行时检查失败#2 - 变量周围的堆栈已损坏

时间:2015-02-18 01:37:09

标签: c++ xml serialization boost

我对Boost序列化非常陌生。我使用Boost来序列化Xml文档:

typedef struct xmllist
{
   std::string Name;
   int Param;

   friend class boost::serialization::access;
   template<class Archive>
   void serialize(Archive& ar, const unsigned int version)
   {
      ar & BOOST_SERIALIZATION_NVP(Name);
      ar & BOOST_SERIALIZATION_NVP(Param);
   }
} XMLLIST;

void XmlWrite()
{
   std::ofstream ofs("xmlfile.xml");
   assert(ofs.good());
   boost::archive::xml_oarchive oa(ofs);
   XMLLIST xmllist;
   xmllist.Name = "Name";
   xmllist.Param = 1;
   oa << BOOST_SERIALIZATION_NVP(xmllist);
}

boost :: archive :: xml_oarchive oa(ofs)给我运行时检查失败#2 - 围绕变量&#39; oa&#39;已经腐败了。

我正在使用Microsoft Visual Studio 2010和boost_1_56_0。

有人可以帮我解决这个问题吗?

1 个答案:

答案 0 :(得分:0)

我认为堆栈损坏不是在提供的代码中发生,而是在其他地方发生。我在下面提供的略微修改的代码在MSVC ++ 2013和Boost 1.57下没有任何错误。它也适用于coliru

#include <string>
#include <sstream>
#include <iostream>
#include <ostream>
#include <cassert>
#include <boost/archive/xml_oarchive.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/serialization/access.hpp>

struct CXMLList {
    std::string Name;
    int Param;

    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & BOOST_SERIALIZATION_NVP(Name);
        ar & BOOST_SERIALIZATION_NVP(Param);
    }
};

int main(int argc, char* argv[]) {
    std::ostringstream ofs;
    boost::archive::xml_oarchive oa(ofs);
    CXMLList xmllist;
    xmllist.Name = "Name";
    xmllist.Param = 1;
    oa << BOOST_SERIALIZATION_NVP(xmllist);
    std::cout << "XML is:" << std::endl;
    std::cout << ofs.str() << std::endl;
    return 0;
}