0

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;
}
Glorfindel
  • 21,988
  • 13
  • 81
  • 109
Dan Bell
  • 33
  • 6
  • For me it created a file with 44 bytes which seems reasonable considering the prelude and tailer that the serialization adds. Can you paste the code used for reproducing the issue as it is ? – Arunmu Sep 06 '16 at 06:29
  • I've added the complete method. It seems it's the saving that is the problem here from what you have said. Clang and Boost, that's my suspicion. – Dan Bell Sep 06 '16 at 06:45
  • Even I am using clang, but not xcode. Would be better to step through debugger. – Arunmu Sep 06 '16 at 06:49

1 Answers1

0
  1. first

    archiveOut << 1;   
    

    You can only use Boost to serialize lvalues. (Look for "stack" in http://www.boost.org/doc/libs/1_61_0/libs/serialization/doc/special.html#objecttracking)

  2. next

    std::ofstream outStream(fileName);
    if(outStream.is_open())
    {
        outStream << 1;
        outStream.close();
        return true;
    }
    

    This doesn't even use boost. The file will be 1 byte on most platforms. If not, it wasn't written or you are looking at the wrong file.

  3. next

    else
    {
        LOG(INFO) << "ERROR: Could not open file stream to save database";
        throw;
    }
    

    Can't rethrow without a pending exception. what does "throw;" outside a catch block do?

Community
  • 1
  • 1
sehe
  • 374,641
  • 47
  • 450
  • 633