c++ - Boost.serialization unregistered class exception with serialized class defined in a runtime-linked shared library -


i trying create modular game system, , user - defined classes able serialized. to this, placing classes derrived polymorphic base class. running troubles while trying implement serialization on class. keep getting unregistered class exception (a runtime error).

here minimal test case:

environment: windows 8.1 msvc++ 12 (visual studio 2013)

parent_class.h -- defines parent_class class polymorphic

#pragma once  #include <boost/serialization/access.hpp> #include <boost/serialization/nvp.hpp> #include <boost/serialization/export.hpp>  class parent_class {  protected:      friend boost::serialization::access;      template <typename archive>     void serialize(archive& ar, const unsigned int version)     {         ar & boost_serialization_nvp(x) & boost_serialization_nvp(y);     }      float x;     float y;  public:      explicit parent_class(float x, float y) : x(x), y(y) {}      // virtual deconstructor make polymorphic     virtual ~parent_class()     {     } };  boost_class_export(parent_class); 

main.cpp - .cpp in .exe

#include "parent_class.h"  #include <boost/archive/xml_oarchive.hpp>  #include <fstream> #include <iostream>  #include <windows.h>  typedef parent_class* addchildfun(float, float, float);  int main() {     // acquire module     hmodule module = loadlibrarya("serializationdll.dll");     assert(module);      // acquire function ptr     farproc addchildraw = getprocaddress(module, "makechild");     assert(addchildraw);     addchildfun* addchildptr = reinterpret_cast<addchildfun*>(addchildraw);      // make polymorphic pointer     parent_class* child = addchildptr(325.f, 214.f, 2.5f);      // init boost serializization archive     std::ofstream stream{ "file.txt" };     boost::archive::xml_oarchive arch{ stream };      try     {          arch << boost_serialization_nvp(child);      }     catch (std::exception& e)     {         std::cout << e.what(); // prints "unregistered class - derived class not registered or exported     }       std::cin.get();     delete child; } 

and here child_class.cpp -- .cpp in .dll

#include <parent_class.h> #include <boost/archive/xml_oarchive.hpp>   class child_class : public parent_class {     friend boost::serialization::access;  public:      float z;      explicit child_class(float x, float y, float z)         : parent_class(x, y),         z(z)     {     }      template <typename archive>     void serialize(archive& ar, const unsigned int version)     {         ar & boost::serialization::make_nvp("owner", boost::serialization::base_object<parent_class>(*this));          ar & boost_serialization_nvp(z);     }      virtual ~child_class() override     {      }  };  // export class boost_class_export(child_class)  // yes using msvc -- hence dllexport extern "c" __declspec(dllexport) parent_class* makechild(float x, float y, float z) {     return new child_class(x, y, z); } 

all of code should pretty self-explanatory -- if have questions, feel free comment.

sorry it's lot of code -- couldn't cut down.

alright, after painstaking amount of effort, found solution, , resided in 2 lines of code.

the problem seemed map stored associations between guid class , implementation defined twice, because resided in .lib file both .dll , .exe linked to. obvious solution using dynamic linking support boost.serialization library.

how use put #define boost_serialization_dyn_link 1 @ beginning of every .cpp file used serialization library. way, code instantiated once, , boost can find class in map.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -