I'm trying to serialize an object of the following type
std::unordered_map<std::vector<Card>, std::unordered_map<InfosetHistory, Node>>& nodeMap
Card
is a struct, InfosetHistory
and Node
are classes which use some other structs as member variables. I have created serialize
functions for all classes that need it. For example here's the one for Card
:
struct Card {
int rank;
int suit;
...
template<class Archive>
void serialize(Archive& ar, const unsigned int version) {
ar & rank;
ar & suit;
}
};
I serialize nodeMap as follows:
std::ofstream file("Strategy" + std::to_string(iteration) + ".bin");
boost::archive::binary_oarchive archive(file);
archive << nodeMap;
file.close();
and deserialize separately like this (Currently choosing to deserialize "Strategy0.bin"):
std::ifstream ifs("Strategy0.bin");
std::unordered_map<std::vector<Card>, std::unordered_map<InfosetHistory, Node>> nodeMap;
if (ifs.good()) {
boost::archive::binary_iarchive ia(ifs);
ia >> nodeMap;
}
When I run the program to create and serialize nodeMap, I am always able to serialize with no issues. The respective .bin files are created, and their sizes seem appropriate for the data I expect them to store.
When I run the program to deserialize nodeMap, however, if the nodeMap isn't that large, I don't have issues, but if it is large, I will get the following error:
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
I assume that this is not actually because of the nodeMap being large, and more because there's a probability of the code creating an entry that is somehow causing problems, and the more entries that are added the greater the probability of running into problems. I read in the Boost documentation that this kind of error can be created because of uninitialized data. I don't believe I have uninitialized data, but I'm not sure how to make sure of that.
In general, I'm unsure how to go about debugging this sort of problem. Any help would be appreciated.
Note: I tried very hard to create a minimal reproducible example, but all the examples I created didn't produce the issue. It's only when I create this sort of object in my program, and add thousands of entries that I run into this kind of problem.
EDIT @sehe asked for some more code. Here are all the relevant sections of the classes and structs relevant to the serialized object:
Note that these classes and structs are in separate files. InfosetHistory
and DrawAction
are declared in InfosetHistory.h
, Node
is declared in Node.h
, and Card
is declared in GRUtil.h
.
@sehe also mentioned that I don't mention the type serialized above. The type being serialized is a reference to the object I'm trying to serialize: std::unordered_map<std::vector<Card>, std::unordered_map<InfosetHistory, Node>>& nodeMap
.
EDIT 2 I have managed to create a minimal reproducible example using the code @sehe provided below. Using my code, I created a NodeMap I knew would produce the deserialization error, and I printed all the data to a text file called "StrategyWritten0.txt". In this reproducible example, I input all the data from that text file to create a NodeMap, serialize all the data in the resulting NodeMap, and then attempt to deserialize the NodeMap. I get the following output:
Successfully created Node Map
Serialized Node Map
terminate called after throwing an instance of 'boost::archive::archive_exception'
what(): input stream error
Here is the file:
https://drive.google.com/file/d/1y4FLgi7f-XWJ-igRq_tItK-pXFDEUbhg/view?usp=sharing
And here is the code: