尝试使用Boost序列化库时出错
Error when trying to use the Boost Serialization library
我制作了一个简单的程序来重现这个问题:
#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
)和boost 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和boost 1.48上进行了测试,它运行良好。不知何故,这一定是我在Mac上的配置特有的问题。
发生了什么:
- Boost是由MacPorts使用内置的Apple GCC 4.2构建的
- 我用非Apple GCC 4.6.2(也是从MacPorts获得的)编译我的程序
- 运行时加载的boost二进制文件与我的二进制文件不兼容
- 撞车
相关文章:
- 如何在C++中序列化结构数据
- 序列化,没有库的整数,得到奇怪的结果
- 如何知道QDataStream不能反序列化某些内容
- 如何使用Python从C++中读取谷物序列化数据
- 如何使用boost::具有嵌套结构和最小代码更改的序列化
- 带有Protobuf序列化的C++Hazelcast:字符串不是UTF-8格式的
- 自定义对象的dlib序列化在gcc中失败
- C++boost序列化多态性问题
- 增强基于 XML class_id的反序列化
- 提升反序列化对象具有 nan 或 -nan 值
- 在 cpp 中的平面缓冲区中序列化对象
- 每次进行继承时都需要提升::序列化::base_object吗?
- 如何在 c++ 非托管代码中反序列化 byte[] 的 json 字符串?
- 提升序列化 1:73 的向后兼容性问题
- 将 boost 序列化对象的 asio::streambuf 表示转换为 Beast 的 DynamicBody req.body()
- 为什么 nlohmann/json 序列化 "null" 而不是在 double 上"0"?
- 提升反序列化期间出错
- 尝试使用Boost序列化库时出错
- 链接到增强序列化时出错
- c++Boost序列化,编译派生类时出错