Boost Binary Deserialization

  binary, boost, c++, deserialization, serialization

I’m creating a D&D engine for fun just to practice my c++ skills and learn some of the more in depth topics. Currently, I am working on building a system to save and load characters. I have a Stats class, that holds all of the statistics for a character, and a character class that currently just has a name and a stats* to a stats object for that character.

So far, I’ve been able to successfully save the data using boost text archive, and now switched to boost binary archive. It appears to work when saving the data, but when I try to load the data I get this error:

"Exception Unhandled – Unhandled exception at [memory address] in VileEngine.exe Microsoft C++ exception: boost::archive::archive_exception at memory location [different mem address]"

I can skip past this error multiple times but when the program runs and loads, the data of the loaded character is way off so I know it has to be either in the way I’m saving it, or more likely in the way I’m loading it. I’ve tried reading through the boost docs but couldn’t find a way to fix it. I also tried searching through other posts but couldn’t find an answer, or maybe I just don’t understand the answers. Any help is greatly appreciated.

Relevant code posted below. I can post all the code if needed but it’s quite a bit for all the classes.

in Character.hpp

    private:
        friend class boost::serialization::access; //allows serialization saving

        //creates the template class used by boost to serialize the classes data
        //serialize is call whenever this class is attempting to be saved
        template<class Archive>
        void serialize(Archive& ar, const unsigned int version) {
            ar << name;
            ar << *charStats;
            ar << inventory;
        }


/*********************************
*       Data Members
***********************************/

        std::string name;
        Stats* charStats;
        std::vector<std::string> inventory;

    public:
        Character();


        void loadCharacter(std::string &charName); //saves all character details
        void saveCharacter(); //loads all character details

in Character.cpp

/*********************************************
Functions to save and load character details
**********************************************/

void Character::saveCharacter() {
    //save all details of character to charactername.dat file
    
    //create filename of format "CharacterName.dat"
    std::string fileName = name + ".dat";
    std::ofstream saveFile(fileName);

    //create serialized archive and save this characters data
    boost::archive::binary_oarchive outputArchive(saveFile);
    outputArchive << this;

    saveFile.close();
        
}

void Character::loadCharacter(std::string &charName) {
    //load details of .dat file into character using the characters name
    std::string fileName = charName + ".dat";
    std::ifstream loadFile(fileName);

    boost::archive::binary_iarchive inputArchive(loadFile);
    inputArchive >> name;

    Stats* temp = new Stats;
    inputArchive >> temp;
    charStats = temp;


    inputArchive >> inventory;

    loadFile.close();

}

in Stats.hpp

private:

    friend class boost::serialization::access; //allows serialization saving

    //creates the template class used by boost to serialize the classes data
    //serialize is call whenever this class is attempting to be saved
    template<class Archive>
    void serialize(Archive& ar, const unsigned int version) {
        ar & skillSet;
        ar & subSkillMap;
        ar & level;
        ar & proficiencyBonus;
    }

Source: Windows Questions C++

LEAVE A COMMENT