I use boost::serialization
for my classes. Since I have some inheritance, I have to use BOOST_CLASS_EXPORT
to "register" my class. Hope I did not misunderstand anything.
I use this macro:
BOOST_CLASS_EXPORT(MyClass)
Or even I use this:
BOOST_CLASS_EXPORT_GUID(MyClass, "MyClass")
in one header file, e.g. MyClass.h, which also contains the definition of MyClass. However, even I tried to put the macro in one of my source (*.cpp) file, it fails again.
I get a segmentation fault at the end of main()
. Stacktrace shows the call stack is almost the same with this problem. I cannot find a solution or workaround for this problem. I think the problem is because of the destructor, but I don't know how to solve it. I even cannot say this is my fault or a bug in boost. (I hope it's my fault that I can fix it by myself)
Is there any solution/workaround for this? And why does the problem occur?
My build environment is: Ubuntu 10.04 64-bit server (kernel 2.6.32), gcc 4.4.3, boost 1.40. (I found it on /usr/include/boost/version.hpp
and use apt-cache show libboost-dev
to see the version)
I am using -Wall -g3 -O0
as the compiler option.
Strangely, the problem occurs on only one of my machines. Another machine running CentOS (kernel 2.6.18), gcc 4.1.2, and boost 1.41 goes well.
Update: I reduce the whole project to a small example. It is put on the bottom and also available on my gist. The classes do nothing, and the program (of mine) does nothing. Nevertheless the BOOST_CLASS_EXPORT
causes segmentation fault.
Notice that if I remove f1.cpp
or f2.cpp
, the problem disappears (multiple compilation unit needed). The similar condition occurs on class A/A_child
and B/B_child
. I have no idea about why the problem does not occur for only one class.
Code
Compile
g++ -Wall -g3 -O0 -o program main.cpp f1.cpp f2.cpp -lboost_serialization
f1.cpp
#include "MyClass.h"
f2.cpp (the same with f1.cpp)
#include "MyClass.h"
main.cpp
int main(int argc, char *argv[])
{
}
MyClass.h
#ifndef MYCLASS_H
#define MYCLASS_H
#include <boost/serialization/export.hpp>
#include <boost/serialization/vector.hpp>
using namespace std;
class A
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
}
};
class A_child: public A
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<A>(*this);
}
};
BOOST_CLASS_EXPORT(A_child)
class B
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
}
};
class B_child: public B
{
public:
template<class Archive>
void serialize(Archive & ar, const unsigned int version)
{
ar & boost::serialization::base_object<B>(*this);
}
};
BOOST_CLASS_EXPORT(B_child)
#endif