I have just started using Cereal and it’s been a treat. It has been fairly easy to learn and it saves me from writing a ton of code. I have one small issue I don’t understand. I’m not sure if it’s related to Cereal or the fact I’m not a very experienced C++ programmer. I’m doing pretty much what the Quick Start example does but I’m trying both the JSON and Binary options and I’m writing to a disk file. Here’s the basic code
if (createMissionFile)
{
{
createStdMission();
std::ofstream missionFileOStreamJSON;
missionFileOStreamJSON.open("MissionDataJSON.crl");
cereal::JSONOutputArchive oarchiveJSON(missionFileOStreamJSON);
oarchiveJSON(cpfMissionData);
std::ofstream missionFileOStreamBin;
missionFileOStreamBin.open("MissionDataBin.crl");
cereal::BinaryOutputArchive oarchiveBin(missionFileOStreamBin);
oarchiveBin(cpfMissionData);
//If I leave this in the code it breaks when trying to deserialize below
missionFileOStreamJSON.close();
missionFileOStreamBin.close();
}
{
std::ifstream missionFileIStreamJSON;
missionFileIStreamJSON.open("MissionDataJSON.crl");
//Here's where it breaks
cereal::JSONInputArchive iarchiveJSON(missionFileIStreamJSON);
MissionDataStruct missionDataInJSON;
iarchiveJSON(missionDataInJSON);
std::ifstream missionFileIStreamBin;
missionFileIStreamBin.open("MissionDataBin.crl");
cereal::BinaryInputArchive iarchiveBin(missionFileIStreamBin);
MissionDataStruct missionDataInBin;
iarchiveBin(missionDataInBin);
}
}
}
When I try to close the ofstream files after I create the output archive as commented in the code above, my app breaks when it gets to the cereal::JSONInputArchive line also commented above. If I comment out the 2 .close() lines, it works fine.
Any ideas why will be greatly appreciated.
Thanks - Gene
Thanks for the quick response. I don’t seem to have permission to reply to your response while I’m logged into the Google group conversation. Hopefully this works.
You’re right I should have been more complete. When I try to close the files in the curly braces everything works fine until the JSONInputArchive line runs. Then I get this error:
Exception thrown at 0x00007FFFCB5ECB69 in CerealConsoleApplication.exe: Microsoft C++ exception: cereal::RapidJSONException at memory location 0x000000BE0971D398.
Unhandled exception at 0x00007FFFCB5ECB69 in CerealConsoleApplication.exe: Microsoft C++ exception: cereal::RapidJSONException at memory location 0x000000BE0971D398.
And Visual Studio 2022 jumps to this line in document.h
MemberIterator MemberEnd() { CEREAL_RAPIDJSON_ASSERT(IsObject()); return MemberIterator(GetMembersPointer() + data_.o.size); }
All that being said, I understand your comment about not needing to close the files inside the curly braces. As a pretty new C++ user, I was just trying to be what I thought was totally explicit. I thought nothing else needed the stream at that point so I closed it. Your explanation about closing the file before the destructor gets called makes sense. Thanks for contributing to my C++ education.