I am currently trying to serialize and deserialize objects using boost. My code does not work, so i have simplified the example and i get the EXACT same result. What am i doing wrong here? Boost obviously works.
Includes
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
Codes
std::ofstream outStream(fileName);
if(outStream.is_open())
{
boost::archive::binary_oarchive archiveOut(outStream);
archiveOut << 1;
outStream.close();
}
This bit is calls fine.. The file IS created BUT it is 1.3MB (!?!?!). That somehow seems very wrong.
std::ifstream inStream(fileName);
if(inStream.is_open())
{
boost::archive::binary_iarchive archiveIn(inStream);
int value = 0;
archiveIn >> value;
inStream.close();
}
The error occurs after the binary_iarchive is instantiated, i can step over this call in the debugger but it crashes on the next step. i.e. After binary_iarchive() but before int value = 0;
boost::archive::binary_iarchive archiveIn(inStream);
*** error: can't allocate region *** set a breakpoint in malloc_error_break to debug libc++abi.dylib: terminating with uncaught exception of type std::bad_alloc: std::bad_alloc
I can't quite see what i'm doing wrong here.
The project is built with clang and developed with XCode.
Are there any common gotchas with boost binary serialization?
What methodologies would help me undercover what i am doing wrong in this code?
Regards
Note:
Other posts are similar, but not quite this problem. I have tried the solution in the above post.
Stackoverflow Boost Serialisation Question
UPDATE
bool saveDatabase(std::string fileName)
{
std::ofstream outStream(fileName);
if(outStream.is_open())
{
boost::archive::binary_oarchive archiveOut(outStream);
archiveOut << 1;
outStream.close();
return true;
}
else
{
LOG(INFO) << "ERROR: Could not open file stream to save database";
}
return false;
}