By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
428,705 Members | 1,301 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 428,705 IT Pros & Developers. It's quick & easy.

Boost serialization: Problem reading strings from binary archive

P: n/a
Hello,

I hope this is the right place to post a question on Boost. If not,
please let me know where I can post this message and I will do so.

I am having a strange problem with std::string as I am trying to read
from a binary archive using Boost serialization. I am new to this, and
it is possible that I have not understood the usage. In the code
below, the string "faultblock" seems to be causing the problem. The
code crashes in the ia & tst line which is reading back the archive -
the stack indicates that it is trying to read an invalid string.

I have tried to minimize the code I need to post, but sorry if this is
still too much.

I'd appreciate any help with this. I am using Boost 1.34.0 on windows
XP.

Best Regards

Mahesh
//
// test.cpp
//
#include <iomanip>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <boost/archive/text_iarchive
..hpp>
#include <boost/archive/text_oarchive.hpp
#include <boost/archive/binary_oarchive.hpp>
#include <boost/archive/binary_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/utility.hpp>
#include <boost/serialization/list.hpp>

#include "Attribute4dv.h"

using namespace std;

class Test
{
private:
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar.template register_type<AttrDouble4dv>();
ar.template register_type<AttrString4dv>();

ar & _attrs;
}

public:

vector<Attribute4dv*_attrs;

Test() {}
~Test() {}

};

int main(int argc, char* arv[])
{
// save data to archive
{
Test* tst = new Test;
vector<doubledVals;
dVals.push_back(1.1);
dVals.push_back(2.2);
AttrDouble4dv* attr1 = new AttrDouble4dv;
attr1->_name = "attr1";
attr1->_vals = dVals;
tst->_attrs.push_back(attr1);

vector<stringlut;
lut.push_back("2001");
lut.push_back("2002");
vector<intlutIndices;
lutIndices.push_back (0);
lutIndices.push_back(1);
AttrString4dv* attr2 = new AttrString4dv;
attr2->_name = "faultblock";
attr2->_indices = lutIndices;
attr2->_lut = lut;
tst->_attrs.push_back(attr2);

const Test* tst2 = tst;
std::ofstream ofs("test.bin");
boost::archive::binary_oarchive oa(ofs);
oa & tst2;
}

// read data from archive
{
Test* tst;
std::ifstream ifs("test.bin", std::ios::binary);
boost::archive::binary_iarchive ia(ifs);
ia & tst;
int j = 0;
}

return 0;
}

//
// Attribute4dv.h
//
#ifndef _ATTRIBUTE4DV_H
#define _ATTRIBUTE4DV_H

#include <vector>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/serialization/string.hpp>
#include <boost/serialization/vector.hpp>

// Base class
class Attribute4dv
{
public:

Attribute4dv() {}
virtual ~Attribute4dv() {}

// name of attribute
std::string _name;

private:

// for serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & _name;
}

};

// double attributes
class AttrDouble4dv : public Attribute4dv
{
public:

AttrDouble4dv() {}
~AttrDouble4dv() {}

std::vector<double_vals;

private:

// for serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// save/load base class info
ar & boost::serialization::base_object<Attribute4dv>(*t his);

ar & _vals;
}
};

// string attributes
class AttrString4dv : public Attribute4dv
{
public:

AttrString4dv() {}
~AttrString4dv() {}

// a lookup table (LUT) of strings
std::vector<std::string_lut;
// indices to the LUT
std::vector<int_indices;

private:

// for serialization
friend class boost::serialization::access;
template<class Archive>
void serialize(Archive &ar, const unsigned int version)
{
// save/load base class info
ar & boost::serialization::base_object<Attribute4dv>(*t his);

ar & _lut;
ar & _indices;
}
};

#endif // _ATTRIBUTE4DV_H

Nov 9 '07 #1
Share this Question
Share on Google+
2 Replies


P: n/a
On Nov 8, 9:57 pm, "mkvenkit...@gmail.com" <mkvenkit...@gmail.com>
wrote:
Hello,

I hope this is the right place to post a question on Boost. If not,
please let me know where I can post this message and I will do so.
This page has more info
http://lists.boost.org/mailman/listinfo.cgi/boost-users

You can send email to bo*********@lists.boost.org
and have this posted there.

Brian Wood
Ebenezer Enterprises
www.webebenezer.net

Nov 9 '07 #2

P: n/a
On Nov 9, 9:13 am, c...@mailvault.com wrote:
....
You can send email to boost-us...@lists.boost.org
and have this posted there.

Brian Wood
Ebenezer Enterpriseswww.webebenezer.net

Thanks - I just emailed the group.

Mahesh

Nov 9 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.