Closing seems to break my code

30 views
Skip to first unread message

Gene Massion

unread,
May 6, 2023, 7:32:03 PM5/6/23
to cere...@googlegroups.com

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

Erich Keane

unread,
May 6, 2023, 7:47:07 PM5/6/23
to cereal serialization library
Without the error message/ type of 'my app breaks' that you mean here, it isn't clear what your problem is.

In the JSON case, the final close-curley brace gets emitted when the archive is destructed.  You're closing the stream before the archive is done with it, so I suspect you are getting a crash that is actually a result of the destructor there, but no idea why you think it is the JSONInputArchive line.

That said, calling 'close' on a stream going out of scope is silly, the destructor of the stream closes the file handle by spec.

Gene Massion

unread,
May 7, 2023, 2:23:04 PM5/7/23
to Gene Massion, cere...@googlegroups.com

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.

Reply all
Reply to author
Forward
0 new messages