
Kolb, Jeremy wrote:
I'm having a lot of trouble getting serialization of derived classes through a base pointer working across dlls.
Take a look at dll_polymorphic_derived2 in the serialization test directory. The suggested changes are as follows: I have the following:
DLL (data_types.dll) code:
// delete global includes
stdafx.h: #pragma once
#include "targetver.h"
#define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers // Windows Header Files: #include
// base doesn't have to be exported
Base.h: #pragma once class __declspec(dllexport) Base { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version);
public: Base() {} virtual ~Base() {} int i; };
BOOST_SERIALIZATION_ASSUME_ABSTRACT(Base); // Doesn't make a difference.
// base serialization needs to be instantiated for all archives to be used
Base.cpp: #include "stdafx.h" #include "Base.h"
template<class Archive> void Base::serialize(Archive& ar, const unsigned int version) { ar & BOOST_SERIALIZATION_NVP(i); }
// instantiate for the "real" archives used - that is polymorphic_text_iarchive, etc. // note that I don't believe that this should be necessary for abstract base class Base. // So first try totally excluding this.
template void __declspec(dllexport) Base::serialize(boost::archive::polymorphic_xml_iarchive& ar, const unsigned int version); template void __declspec(dllexport) Base::serialize(boost::archive::polymorphic_xml_oarchive& ar, const unsigned int version);
// be sure to include any other archives which might be used (text, binary, etc) // Derived classes MUST be exported.
Derived.h: #include "Base.h"
class __declspec(dllexport) Derived : public Base { friend class boost::serialization::access; template<class Archive> void serialize(Archive& ar, const unsigned int version);
public: Derived() {} Derived(int num) : j(num) {} int j; };
BOOST_CLASS_EXPORT_KEY(Derived);
Derived.cpp: #include "stdafx.h" #include "Derived.h"
template<class Archive> void Derived::serialize(Archive& ar, const unsigned int version) { ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(Base); ar & BOOST_SERIALIZATION_NVP(j); }
BOOST_CLASS_EXPORT_IMPLEMENT(Derived);
// instantiate for the "real" archives used - that is polymorphic_xml_iarchive, etc.
template void __declspec(dllexport) Derived::serialize(boost::archive::polymorphic_xml_iarchive& ar, const unsigned int version); template void __declspec(dllexport) Derived::serialize(boost::archive::polymorphic_xml_oarchive& ar, const unsigned int version);
My exe links against the DLL and contains the following:
// should run
#include "stdafx.h" #include "boost/archive/polymorphic_xml_iarchive.hpp" #include "boost/archive/polymorphic_xml_oarchive.hpp"
#include "data_types/Derived.h" #include <fstream>
int _tmain(int argc, _TCHAR* argv[]) { std::ofstream f("c:\\test.xml"); boost::archive::polymorphic_xml_oarchive xml(f);
Base* b = new Derived(123); xml & BOOST_SERIALIZATION_NVP(b);
delete b;
return 0; }
Robert Ramey