Boost logo

Boost :

Subject: [boost] [serialization] Can one deserialize a derived classes via pointers to the base class using polymorphic archives in a DLL context?
From: Bogdan (bogdan.indy_at_[hidden])
Date: 2010-02-12 21:48:24


Hi all,

I am trying to upgrade to boost 1.42 using Visual Studio 2008 and I have
difficulties using serialization with polymorphic archives in a DLL setup.

I would like to encapsulate in one DLL the class serialization functionality
based on a polymorphic base archive. In another place, I would like to
perform the actual serialization/deserialization using a specific archive
(e.g., polymorphic_text_[io]archive).

The section "Special Considerations" of the documentation indicates in two
paragraphs ("DLLS - Serialization and Runtime Linking" and " Plugins") that
this scenario is supported by the library. However I couldn't find any
example that shows how to do that. Therefore, I changed slightly the boost
test that includes test_dll_exported.cpp and polymorphic_derived2.cpp as
follows:

1.

I moved the classes polymorphic_base and polymorphic_derived1 to the project
polymorphic_derived2

This way, we have one place where those classes serialize their members
(i.e., polymorphic_derived2.dll) and a separate binary where we serialize
them via a derived archive (i.e., test_main() inside test_dll_exported.exe);

2.
I replaced the template version of the serialization functions with the
equivalent version based on boost::archive::polymorphic_[io]archives. I also
removed the text-based instantiation inside the Dll.

As indicated in the boost documentation, the class serialization will only
have to be compiled once for all archive implementations (present or
future). Therefore, the DLL should contain only the serialization functions
to the base polymorphic archives.

While everything builds correctly, at run time I get a C++ exception BEFORE
main() in basic_serializer_map.cpp around the line 48:

    // if this fails, it's because it's been instantiated
    // in multiple modules - DLLS - a recipe for problems.
    // So trap this here
    if(!result.second){
        boost::serialization::throw_exception(
            archive_exception(
                archive_exception::multiple_code_instantiation,
                bs->get_debug_info()
            )
        );
    }

Please note BOOST_CLASS_EXPORT_IMPLEMENT is used only once per class (inside
polymorphic_derived2.cpp), yet the serializers are inserted in the same map
both from the Dll and from the executable.

Am I doing something wrong? Has anyone been able to create a similar working
example? I really appreciate any feedback I could get.

Thank you,

Bogdan

PS
For more info I attached the files describing my problem plus the .sln file
(I used the configuration "Debug runtime-dynamic threading-multi")




Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk