I'm using the example provided with Boost.MPI documentation found here. I have a bit modified these two classes and as soon as I add destructor to this class, I will get segmentation fault during receiving a std::vector<drivedClass>
. How can I resolve this? This class doesn't have any pointer and everything is defined statically, so default destructor is fine. I'm compiling this with : mpic++ mpidatatype.cpp -o out -lboost_mpi -lboost_serialization
The example is :
#include <boost/mpi.hpp>
#include <boost/serialization/base_object.hpp>
#include <boost/archive/text_oarchive.hpp>
#include <boost/archive/text_iarchive.hpp>
#include <boost/serialization/vector.hpp>
#include <iostream>
using namespace boost::archive;
std::stringstream ss;
class animal
{
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version) {
ar & legs_;
ar& arms_;
}
protected:
int legs_;
int arms_;
public:
animal() = default;
animal(const int legs) : legs_{legs},arms_{2} {}
int legs() const { return legs_; }
virtual void printName() = 0;
virtual ~animal()=default;
};
class bird : public animal
{
public:
bird() = default;
bird(int legs, bool can_fly) :
animal{legs}, can_fly_{can_fly} {}
bool can_fly() const { return can_fly_; }
void printName() {
std::cout<<"My name is bird"<<std::endl;
};
virtual ~bird()=default;
private:
friend class boost::serialization::access;
template <typename Archive>
void serialize(Archive &ar, const unsigned int version)
{
ar & boost::serialization::base_object<animal>(*this);
ar & can_fly_;
}
bool can_fly_;
};
namespace boost { namespace mpi {
template <>
struct is_mpi_datatype<bird> : mpl::true_ {};
}}
int main(int argc, char* argv[])
{
boost::mpi::environment env;
boost::mpi::communicator world;
bool isBirdMPIType =boost:: mpi::is_mpi_datatype<bird>::value;
std::cout << "Is bird a valid MPI datatype? " << std::boolalpha << isBirdMPIType << std::endl;
if (world.size() > 1)
{
if (world.rank() == 0)
{
//std::vector<bird> dummy ={bird (1,0)};
std::vector<bird> dummy ={bird (1,0), bird(2,1)};
world.send(1, 0, dummy);
}
if (world.rank() == 1)
{
std::vector<bird> dummy(2, bird(1,1));
// std::vector<bird> dummy(1, bird(1,1));
world.recv(0, 0, dummy);
}
}
}
I have provided this example as a demo. In the real code that I have, I don't have any pointer as member class and destructors are all default, it took me hours to figure out destructor of the class was the cause of segmentation fault. Questions about this all revolved around having a pointer and not handling that correctly in destructor but when the class doesn't have any, why this should be a problem?