尝试使用 Boost 序列化库时出错

发布于 2024-12-29 13:21:06 字数 2187 浏览 1 评论 0原文

我制作了一个简单的程序来重现该问题:

#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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

弥枳 2025-01-05 13:21:06

事情是这样的:

  1. Boost 是由 MacPorts 使用内置的 Apple GCC 4.2 构建的。
  2. 我正在使用非 Apple GCC 4.6.2(也从 MacPorts 获得)编译我的程序。
  3. 在运行时加载的 boost 二进制文件与我的二进制文件不兼容。
  4. 碰撞!

What happened was this:

  1. Boost was built by MacPorts using the built-in Apple GCC 4.2.
  2. I was compiling my program with non-Apple GCC 4.6.2 (also obtained from MacPorts).
  3. The boost binary that was loaded at runtime was not compatible with my binary.
  4. Crash!
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文