我制作了一个简单的程序来重现问题:
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/tuple/tuple.hpp>
#include <sstream>
#include <iostream>
template<typename T>
std::string serialize(const T & value)
{
std::ostringstream oss;
boost::archive::text_oarchive oa(oss);
oa << value;
return oss.str();
}
template<typename T>
T deserialize(const std::string & buffer)
{
std::istringstream iss(buffer);
boost::archive::text_iarchive ia(iss);
T ret;
ia >> ret;
return ret;
}
struct MyClass
{
MyClass() {}
MyClass(const std::string & name) : name(name) {}
template<class Archive>
void serialize(Archive & ar, const unsigned int)
{
ar & name;
}
std::string name;
};
int main()
{
MyClass myClass("Test");
std::string serialized = serialize(myClass);
std::cout << "Serialized: " << serialized << std::endl;
MyClass deserialized = deserialize<MyClass>(serialized);
std::cout << "Name after deserialization: " << deserialized.name << std::endl;
}
代码编译正常,但在运行时出现以下错误:
Serialized: 22 serialization::archive 9 0 0 4 Test
test(74010) malloc: *** error for object 0x109bf55e0: pointer being freed was not allocated
*** set a breakpoint in malloc_error_break to debug
Abort trap: 6
在调试器中,我可以看到当boost尝试反序列化name
变量时发生错误。
任何人都可以帮我弄清楚我做错了吗?
我使用的是Mac OS X Lion并使用GCC 4.6.2(g++-mp-4.6 (GCC) 4.6.2
)并升级版本1.48。两者都通过MacPorts安装。
命令行是:
g++ -o test -I/opt/local/include -L/opt/local/lib main.cpp -lboost_serialization
您可以在此处查看来自subversion的代码:http://stacked-crooked.googlecode.com/svn/trunk/Playground/Serialization。
我在Linux GCC 4.6.1上测试并提升1.48并且它工作正常。它在某种程度上必须是我在Mac上配置的特定问题。
答案 0 :(得分:0)
发生了什么事: