Search code examples
c++serializationboostboost-serialization

Boost serialization of vectors, crash in release build


I am trying basic Boost serialization examples. While trying to pass vectors I am facing an issue with this code :

#include <iostream>
#include <vector>
#include <fstream>
#include <sstream>
#include <queue>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/list.hpp>
#include <boost/serialization/vector.hpp>

using namespace std;
stringstream ss (stringstream::in | stringstream::out);

void save()
{
  boost::archive::text_oarchive oa(ss);

  vector<int> v;
  v.push_back( 228 );
  v.push_back( 322 );

  oa << BOOST_SERIALIZATION_NVP(v);
}

void load()
{
  boost::archive::text_iarchive ia(ss);
  vector<int> tV;
  ia >> BOOST_SERIALIZATION_NVP(tV) ;
  std::cout << "Hello" << std::endl ;
}

int main()
{    
    save();
    load();

   return 0;
}

In release mode : Program crashes without printing the Hello message.

In Debug mode : Program prints the Hello message and closes gracefully. I saw the value of vector tV in debug mode, it was showing the correctly filled values.

What is it that I might be doing wrong ?

P.S. : I did check similar problem on SO, but couldn't really relate to my issue.


Edit as per comments:

void save()
{
  boost::archive::text_oarchive oa(ss);

  vector<int> v;
  v.push_back( 228 );
  v.push_back( 322 );

  oa << v;
}

void load()
{
  boost::archive::text_iarchive ia(ss);
  vector<int> ;
  ia >> v ;
  std::cout << "Hello" << std::endl ;
}

Solution

  • My guess is that you've got an ODR violation in release mode.

    Ensure that you build your program with exactly the same macros that serialization lib was built with. In particular, pay attention to _SECURE_SCL macro.