c++c++11boostboost-serialization

C++ Boot serialization cause a segmentation fault


I have those two classes and I'm trying to deserialize them using 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;
};

Here I'm creating a world in this way

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];
 }

and here im using this function to serialize the world

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

So the serialize_world function is working without issues and i am inserting it value to mysql longblob.

But now when I'm trying to get the blob from MySql and deserialize it back by using this function

Worlds deserialize(std::string world)
{

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

I'm getting a Segmentation fault (core dumped) I don't know what's wrong.

Thanks.


Solution

  • Looks like you never return a value from generateWorld.

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

    Also in deserialize you never initialize items to anything. That is going to lead to 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.

    Don't use raw new/delete

    See also e.g. 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++

    Just use std::array then and be happy:

    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";
    }
    

    Prints

    Worlds equal? true