473,327 Members | 2,081 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

boost serialization problem with reference members

I have been trying out boost's serialization library for the past few
days. And I have come across a problem serializing a class that has a
reference member. The code is posted as below:

[code]
#include <iostream>
#include <fstream>
#include <string>

#include <boost/archive/xml_iarchive.hpp>
#include <boost/archive/xml_oarchive.hpp>

//for name value pairs when doing XML archiving
#include <boost/serialization/nvp.hpp>

class MyClassWithNoDefaultConstructor;
std::ostream& operator<<(std::ostream& os, const
MyClassWithNoDefaultConstructor& object);

//global integer variable... whose reference will be a member of
MyClassWithNoDefaultConstructor
int global_int = 10;

class MyClassWithNoDefaultConstructor
{
public:
MyClassWithNoDefaultConstructor(int i, int& ref_)
: intMember(i), ref(ref_)
{}
private:
int intMember;
int& ref;
template<class Archive>
void serialize(Archive& ar, const unsigned int version)
{
ar & BOOST_SERIALIZATION_NVP(intMember);
ar & BOOST_SERIALIZATION_NVP(ref);
}
friend class boost::serialization::access;
friend std::ostream& operator<<(std::ostream& os, const
MyClassWithNoDefaultConstructor& object);
};

std::ostream& operator<<(std::ostream& os, const
MyClassWithNoDefaultConstructor& object)
{
os << "\nMyClassWithNoDefaultConstructor contents:\n";
os << "intMember - " << object.intMember << "\n";
os << "ref - " << object.ref << "\n";
return os;
}

namespace boost
{
namespace serialization
{
template<class Archive>
inline void save_construct_data(Archive & ar, const
MyClassWithNoDefaultConstructor* t, const unsigned int file_version)
{
// save data required to construct instance
ar << t->intMember;
ar << &(t->ref);
}

template<class Archive>
inline void load_construct_data(Archive & ar,
MyClassWithNoDefaultConstructor* t, const unsigned int file_version)
{
// retrieve data from archive required to construct new instance
int m;
//int * ptr;
int * ptr = new int();
ar >m;
ar >ptr;
::new(t)MyClassWithNoDefaultConstructor(m, *ptr);
}
}
}

void SerializeMyClassWithNoDefaultConstructor(const std::string&
filename)
{
MyClassWithNoDefaultConstructor object(111, global_int);
std::ofstream ofs(filename.c_str());
assert(ofs.good());
boost::archive::xml_oarchive xml_oa(ofs);
xml_oa << BOOST_SERIALIZATION_NVP(object);
}

void DeserializeMyClassWithNoDefaultConstructor(const std::string&
filename)
{
char * buffer = new char[sizeof(MyClassWithNoDefaultConstructor)];
MyClassWithNoDefaultConstructor* ptr =
reinterpret_cast<MyClassWithNoDefaultConstructor*> (buffer);
std::ifstream ifs(filename.c_str());
assert(ifs.good());
std::cout << "inside deserialize()" << std::endl;
boost::archive::xml_iarchive xml_ia(ifs);
std::cout << "xml_iarchive constructed" << std::endl;
xml_ia >BOOST_SERIALIZATION_NVP(*ptr);
std::cout << "deserialized" << std::endl;
std::cout << *ptr;
ptr->~MyClassWithNoDefaultConstructor();
delete[] buffer;
buffer=NULL; ptr=NULL;
}

int main()
{
const std::string filenameMyClassWithNoDefaultConstructor="/tmp/
testfileMyClassWithNoDefaultConstructor.xml";
try
{

SerializeMyClassWithNoDefaultConstructor(filenameM yClassWithNoDefaultConstructor);

DeserializeMyClassWithNoDefaultConstructor(filenam eMyClassWithNoDefaultConstructor);
}
catch(const boost::archive::archive_exception& ex)
{
std::cout << ex.what() << "\n";
}
catch(const std::exception& ex)
{
std::cout << ex.what() << "\n";
}
return 0;
}

It compiles fine with g++ 3.4.6. But fails with a segmentation fault.
I tried fiddling around with it but the best I can do is cause it to
fail with a bus error or a stream error. Here is the documentation
that I am referring to - http://www.boost.org/libs/serialization/doc/index.html.
For reference members, the specific documentation page is -
http://www.boost.org/libs/serializa...html#references

The code works (or atleast it seems to) if you remove the reference
member. That is, I have it working for classes that don't have a
default constructor but what if there are constant or reference
members? The documentation seems to suggest it is possible to handle
them as are non-default constructor classes are, but I am not able to
get it to work.

Am I doing something silly there? Or is there a major gap between my
understanding and what the documentation says? I am using boost 1.32 -
can that be a reason? Can anyone help with the problem?

Feb 21 '07 #1
0 1623

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: eyal.susser | last post by:
Hi, I'm using the Boost serialization library. Iwant to serialize classes into simple buffers on RAM. There doesn't seem to be a suitable archiver. Am I missing it? The ones I saw were for...
1
by: Hardy | last post by:
Hi, just come into the boost world. just the first.cpp in the program_options examples, with many link error... devc++4.9.9.2, gcc 3.4.2, can I get your opinions on this problem? thank you~ ...
8
by: Joe | last post by:
I have several classes which I need serialized. Here is their class definitions: public class TopContainer { private LevelTwoType m_levelTwo; public LevelTwoType LevelTwo { get {
0
by: Shane Story | last post by:
My other thread on serialization seemed to be stagnant and I still have questions, so I decided to post this example of what I need to know. I am trying to use binaryformatter. I have no trouble...
6
by: George M. Garner Jr. | last post by:
VC8.0 crashes while compiling boost-1.33.1 serialization library (or any other library that includes the serialization headers) if code analysis (/analyze) is enabled. This problem did not occur...
0
by: Abhishek Padmanabh | last post by:
I have been trying out boost's serialization library for the past few days. And I have come across a problem serializing a class that has a reference member. The code is posted as below: ...
4
by: mijalko | last post by:
Hi, I have inherited my class from System.Drawing.Printing.PrintDocument and I wish to serialize this object using XmlSerializer. And I get exception "There was an error reflecting type ...". If I...
0
by: bharathreddy | last post by:
Before going to that i want to say few thing on serialization : Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an...
2
by: mkvenkit.vc | last post by:
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...
1
by: MindWrapper | last post by:
boost serialization of polymorph classes from DLLs Folks, Let's consider following code -------------------------------- // base.h // abstract base class class IBase {
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.