Boost logo

Boost Users :

Subject: [Boost-users] Some questions about using BOOST_CLASS_EXPORT_KEY macro of boost.serialization library
From: ZouLei (zoulei.vresearch.org_at_[hidden])
Date: 2010-02-26 05:01:35


Dear all ,

The library version I used was boost_1_42, and the IDE was VS2008 . When I
attempt to use the BOOST_CLASS_EXPORT_KEY macro, I met some problems. In my
word, I think it doesn't work as expected . My project files are listed
following (just five tiny files) :

 

// Base.h

#ifndef _BASE_H

#define _BASE_H

 

#include <boost/serialization/access.hpp>

#include <boost/serialization/export.hpp>

#include <boost/serialization/type_info_implementation.hpp>

#include <boost/serialization/extended_type_info_no_rtti.hpp>

#include <boost/serialization/base_object.hpp>

#include <boost/serialization/shared_ptr.hpp>

 

class Base

{

public:

    Base() ;

    Base(int a) ;

    virtual ~Base();

 

private:

    friend class boost::serialization::access ;

 

    template<class Archive>

    void serialize(Archive& ar, const unsigned int version)

    {

        ar & a_ ;

    }

 

private:

    int a_ ;

};

BOOST_CLASS_EXPORT_KEY(Base)

BOOST_SERIALIZATION_SHARED_PTR(Base)

#endif //_BASE_H

 

 

// Base.cpp

#include "Base.h"

 

Base::Base(): a_(0){ }

Base::Base( int a ): a_(a){ }

Base::~Base(){ }

BOOST_CLASS_EXPORT_IMPLEMENT(Base)

 

 

//Derived.h

#ifndef _DERIVED_H

#define _DERIVED_H

 

#include "base.h"

 

class Derived :

    public Base

{

public:

    Derived();

    Derived(int a, int b);

    ~Derived();

 

private:

    friend class boost::serialization::access ;

 

    template<class Archive>

    void serialize(Archive& ar, const unsigned int version)

    {

        ar & boost::serialization::base_object<Base>(*this) ;

        ar & b_ ;

    }

 

private:

    int b_ ;

};

BOOST_CLASS_EXPORT_KEY(Derived)

BOOST_SERIALIZATION_SHARED_PTR(Derived)

#endif // _DERIVED_H

 

 

// Drived.cpp

#include "StdAfx.h"

#include "Derived.h"

 

Derived::Derived(): b_(0){ }

Derived::Derived( int a, int b ): Base(a), b_(b){ }

Derived::~Derived(){ }

BOOST_CLASS_EXPORT_IMPLEMENT(Derived)

 

 

 

// main.cpp

#include <fstream>

#include <iostream>

#include <boost/archive/text_oarchive.hpp>

#include <boost/serialization/shared_ptr.hpp>

 

#include "Derived.h"

 

int _tmain(int argc, _TCHAR* argv[])

{

 

    boost::shared_ptr<Base> base_ptr(new Derived(10,15)) ;

    std::ofstream ofs("test.txt") ;

    boost::archive::text_oarchive oa(ofs) ;

    oa << base_ptr ;

    return 0;

}

 

 

There are no errors in compile-time. But when I executed the program, a
memory error caused ! I don't know what is wrong about my program. Whether
my usage of BOOST_CLASS_EXPORT_KEY/BOOST_CLASS_EXPORT_IMPLEMENT was wrong ?
And, how can I fix the problems ? Thanks, and with my best wishes.

 

Lei

 



Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net