C++ 启动序列化导致分段错误 不要使用原始的 new/delete

问题描述

我有这两个类,我正在尝试使用 boost 反序列化它们

class WorldItem
{
private:
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive &ar,const unsigned int version)
    {
        ar &foreground;
        ar &background;
        ar &breakLevel;
        ar &breakTime;
        ar &water;
        ar &fire;
        ar &glue;
        ar &red;
        ar &green;
        ar &blue;
    }

public:
    int foreground = 0;
    int background = 0;
    int breakLevel = 0;
    long long int breakTime = 0;
    bool water = false;
    bool fire = false;
    bool glue = false;
    bool red = false;
    bool green = false;
    bool blue = false;
};

class Worlds
{
private:
    friend class boost::serialization::access;
    template <class Archive>
    void serialize(Archive &ar,const unsigned int version)
    {
        ar &width;
        ar &height;
        ar &name;
        for (int i = 0; i < 100 * 60; i++)
        {
            ar &items[i];
        }
        ar &owner;
        ar &weather;
        ar &isPublic;
        ar &isNuked;
    }

public:
    int width;
    int height;
    string name;
    WorldItem *items;
    string owner = "";
    int weather = 0;
    bool isPublic = false;
    bool isNuked = false;
};

在这里,我以这种方式创造了一个世界

Worlds generateWorld(string name,int width,int height)
{
    Worlds world;
    world.name = name;
    world.width = width;
    world.height = height;
    world.items = new WorldItem[100 * 60];
 }

在这里我使用这个函数来序列化世界

std::stringstream serialize_world(Worlds world)
{
    std::stringstream str;
    {
        boost::archive::binary_oarchive oa(str);
        oa << world;
    }
    return str;
}

因此,serialize_world 函数正常工作,我将其值插入到 mysql longblob。

但是现在当我尝试从 MySql 中获取 blob 并使用此函数将其反序列化时

Worlds deserialize(std::string world)
{

    Worlds wld;
    std::istream *blobdata = WORLD_DATA(world);
    {
    boost::archive::binary_iarchive ia(*blobdata);
    ia >> wld;
    }
    return wld;
}

我遇到了分段错误(核心已转储)我不知道出了什么问题。

谢谢。

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)