尝试使用 Boost 序列化库时出错
我制作了一个简单的程序来重现该问题:
#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 上的配置特有的问题。
I have a made a simple program that reproduces the problem:
#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;
}
The code compiles fine, but gives the following error at runtime:
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
In the debugger I can see that the error occurs when boost tries to deserialize the name
variable.
Can anyone help me figure out what I'm doing wrong?
Update
I'm on Mac OS X Lion and using GCC 4.6.2 (g++-mp-4.6 (GCC) 4.6.2
) and boost version 1.48. Both installed via MacPorts.
Command line is:
g++ -o test -I/opt/local/include -L/opt/local/lib main.cpp -lboost_serialization
You can checkout the code from subversion here: http://stacked-crooked.googlecode.com/svn/trunk/Playground/Serialization .
Update
I tested on Linux GCC 4.6.1 and boost 1.48 and it works fine. It somehow must be an issue specific to my configuration on Mac.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
事情是这样的:
What happened was this: