C++ 引导序列化导致分段错误

C++ Boot serialization cause a segmentation fault

我有这两个 类,我正在尝试使用 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;
}

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

谢谢。

看起来你从来没有 return 来自 generateWorld 的值。

My compiler warns about this. Try enabling your compiler's diagnostics. I usually have -Wall -Wextra -pedantic enabled

此外,在 deserialize 中,您永远不会将 items 初始化为任何东西。这将导致 UB.

This, too, could be diagnosed by most compilers (-fsanitize=address,undefined helps, although it makes compilation and runtime slow). There's also external tools like Valgrind that do these

Finally, I have no idea what is going on with blobdata, so I'm going to ignore that, but it too looks wrong.

不要使用原始 new/delete

另见例如https://www.quora.com/Why-are-the-%E2%80%98new%E2%80%99-and-%E2%80%98delete%E2%80%99-keywords-considered-bad-in-modern-C++

然后使用 std::array 并快乐:

Live On Coliru

#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <boost/serialization/serialization.hpp>
#include <boost/serialization/array.hpp>
#include <array>
#include <iostream>
#include <sstream>

class WorldItem {
  private:
    friend class boost::serialization::access;
    template <class Ar> void serialize(Ar& ar, unsigned) {
        ar& foreground& background& breakLevel& breakTime& water& fire& glue&
            red& green& 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;

    auto operator<=>(WorldItem const&) const = default;
};

class Worlds
{
  private:
    friend class boost::serialization::access;
    template <class Ar> void serialize(Ar& ar, unsigned) {
        ar& width& height& name& items& owner& weather& isPublic& isNuked;
    }

  public:
    int width;
    int height;
    std::string name;

    std::array<WorldItem, 100 * 60> items;
    std::string owner = "";
    int weather       = 0;
    bool isPublic     = false;
    bool isNuked      = false;

    auto operator<=>(Worlds const&) const = default;
};
//So here im creating a world in this way

Worlds generateWorld(std::string name, int width, int height) {
    Worlds world;
    world.name   = name;
    world.width = width;
    world.height = height;
    return world;
}

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

Worlds deserialize(std::string world) {
    Worlds wld;
    std::istringstream blobdata(world);
    {
        boost::archive::binary_iarchive ia(blobdata);
        ia >> wld;
    }
    return wld;
}

int main() {
    Worlds w = generateWorld("test", 6, 6);

    Worlds clone = deserialize(serialize_world(w));

    std::cout << "Worlds equal? " << std::boolalpha << (w == clone) << "\n";
}

版画

Worlds equal? true