Boost logo

Boost Users :

Subject: Re: [Boost-users] [serialization] polymorphic archives in dllsandpointer to derived problems
From: Kolb, Jeremy (jkolb_at_[hidden])
Date: 2011-01-04 14:43:34


Thanks for the help but I don't quite follow:

A) Isn't the point of using the polymorphic types is to avoid
instantiating all the types explicitly (at the cost of
performance/virtual function calls)?

Anyway I can't seem to instantiate the polymorphic_xml_oarchive
explicitly without getting linker errors:

#include "boost/archive/polymorphic_xml_oarchive.hpp"
template void __declspec(dllexport)
Base::serialize(boost::archive::polymorphic_xml_oarchive& ar, const
unsigned int version);

BOOST_CLASS_EXPORT_IMPLEMENT(Base);

1>Base.obj : error LNK2019: unresolved external symbol "public: static
bool __cdecl boost::archive::detail::archive_serializer_map<class
boost::archive::detail::polymorphic_oarchive_route<class
boost::archive::xml_oarchive_impl<class boost::archive::xml_oarchive> >
>::insert(class boost::archive::detail::basic_serializer const *)"
(?insert@?$archive_serializer_map_at_V?$polymorphic_oarchive_route_at_V?$xml_o
archive_impl_at_Vxml_oarchive_at_archive@boost@@@archive_at_boost@@@detail_at_archiv
e_at_boost@@@detail_at_archive@boost@@SA_NPBVbasic_serializer_at_234@@Z)
referenced in function "public: __thiscall
boost::archive::detail::pointer_oserializer<class
boost::archive::detail::polymorphic_oarchive_route<class
boost::archive::xml_oarchive_impl<class boost::archive::xml_oarchive>
>,class Base>::pointer_oserializer<class
boost::archive::detail::polymorphic_oarchive_route<class
boost::archive::xml_oarchive_impl<class boost::archive::xml_oarchive>
>,class Base>(void)"
(??0?$pointer_oserializer_at_V?$polymorphic_oarchive_route_at_V?$xml_oarchive_
impl_at_Vxml_oarchive_at_archive@boost@@@archive_at_boost@@@detail_at_archive@boost@
@VBase@@@detail_at_archive@boost@@QAE_at_XZ)
1>Base.obj : error LNK2019: unresolved external symbol "public: static
void __cdecl boost::archive::detail::archive_serializer_map<class
boost::archive::detail::polymorphic_oarchive_route<class
boost::archive::xml_oarchive_impl<class boost::archive::xml_oarchive> >
>::erase(class boost::archive::detail::basic_serializer const *)"
(?erase@?$archive_serializer_map_at_V?$polymorphic_oarchive_route_at_V?$xml_oa
rchive_impl_at_Vxml_oarchive_at_archive@boost@@@archive_at_boost@@@detail_at_archive
@boost@@@detail_at_archive@boost@@SAXPBVbasic_serializer_at_234@@Z) referenced
in function "public: __thiscall
boost::archive::detail::pointer_oserializer<class
boost::archive::detail::polymorphic_oarchive_route<class
boost::archive::xml_oarchive_impl<class boost::archive::xml_oarchive>
>,class Base>::~pointer_oserializer<class
boost::archive::detail::polymorphic_oarchive_route<class
boost::archive::xml_oarchive_impl<class boost::archive::xml_oarchive>
>,class Base>(void)"
(??1?$pointer_oserializer_at_V?$polymorphic_oarchive_route_at_V?$xml_oarchive_
impl_at_Vxml_oarchive_at_archive@boost@@@archive_at_boost@@@detail_at_archive@boost@
@VBase@@@detail_at_archive@boost@@QAE_at_XZ)

B) I do not see a dll_polymorphic_derived2 test in 1.44 (which is what
I'm using) or trunk. I do see a polymorphic_derived2.cpp but I don't
see anything obviously wrong with what that has vs what my file has.

Note that I'm definitely getting into
guid_initializer<Derived>::export_guid() in the DLL.

Can I attach files to the list? I have a vs2008 project that I can
post. Thanks.

Jeremy

-----Original Message-----
From: boost-users-bounces_at_[hidden]
[mailto:boost-users-bounces_at_[hidden]] On Behalf Of Robert Ramey
Sent: Tuesday, January 04, 2011 1:29 PM
To: boost-users_at_[hidden]
Subject: Re: [Boost-users] [serialization] polymorphic archives in
dllsandpointer to derived problems

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 <windows.h>
>

// 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

_______________________________________________
Boost-users mailing list
Boost-users_at_[hidden]
http://lists.boost.org/mailman/listinfo.cgi/boost-users


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