[serialization] assert when serializing polymorphic type to XML (Visual Studio 8)
I have condensed a short example which is appended to this message. The code
tries to write then read a polymorphic class via a base pointer. The
assertion I am seeing is assert(NULL != bpos_ptr); in oserializer.hpp. The
documentation indicates that the problem is likely a lack of registration of
the class, however, I have done this. Interestingly the code only fails for
XML archive type.
// BoostPolymorphicSerialize.cpp : Defines the entry point for the console
application.
//
#include
Terence Wilson wrote:
I have condensed a short example which is appended to this message. The code tries to write then read a polymorphic class via a base pointer. The assertion I am seeing is assert(NULL != bpos_ptr); in oserializer.hpp. The documentation indicates that the problem is likely a lack of registration of the class, however, I have done this. Interestingly the code only fails for XML archive type.
// BoostPolymorphicSerialize.cpp : Defines the entry point for the console application. //
#include
#include #include <iomanip> #include <iostream> #include <fstream> #include <string>
#include
#include
#include #include
#include #include #include #include
#include #include #include "boost/archive/xml_iarchive.hpp" #include "boost/archive/xml_oarchive.hpp"
class CMyBaseClass { public: CMyBaseClass(void):m_BaseValue(0){} public: virtual ~CMyBaseClass(void){} int m_BaseValue;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */) { ar & BOOST_SERIALIZATION_NVP(m_BaseValue); } };
class CMyDerivedClass : public CMyBaseClass { public: CMyDerivedClass(void):m_DerivedValue(1){} public: ~CMyDerivedClass(void){} int m_DerivedValue;
friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int /* file_version */) { BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyBaseClass); ar & BOOST_SERIALIZATION_NVP(m_DerivedValue); } };
BOOST_CLASS_EXPORT_GUID(CMyBaseClass, "CMyBaseClass") BOOST_CLASS_EXPORT_GUID(CMyDerivedClass, "CMyDerivedClass")
int _tmain(int argc, _TCHAR* argv[]) { const CMyBaseClass* const pMyBaseClass=new CMyDerivedClass(); const std::string fileName("c:\\docs\\BoostSerializeTest.xml"); CMyBaseClass* pMyBaseClass2=0;
try { { std::ofstream ofs(fileName.c_str()); assert(ofs.good()); boost::archive::xml_oarchive oa(ofs); //boost::archive::text_oarchive oa(ofs); oa << BOOST_SERIALIZATION_NVP(pMyBaseClass); }
{ std::ifstream ifs(fileName.c_str()); assert(ifs.good()); boost::archive::xml_iarchive ia(ifs); //boost::archive::text_iarchive ia(ifs); ia >> BOOST_SERIALIZATION_NVP(pMyBaseClass2); } } catch(std::exception& e) { std::cout << e.what() << std::endl; }
delete pMyBaseClass; delete pMyBaseClass2;
return 0; }
Note that for "export" to work, the boost/serialization/export.hpp header must follow any archive headers. So moving the xml_iarchive.hpp .. up to where the text_iarchive.hpp headers are included should address the problem. Robert Ramey
OK, that did it, but upon examination of the xml output I noticed the base class is not being serialized. A breakpoint inserted in CMyBaseClass::serialize never gets hit. Any thoughts?
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Wednesday, January 04, 2006 8:09 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [serialization] assert when serializingpolymorphictype to XML (Visual Studio 8)
Note that for "export" to work, the boost/serialization/export.hpp header must follow any archive headers. So moving the xml_iarchive.hpp .. up to where the text_iarchive.hpp headers are included should address the problem.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Look in the documentation under "base_object" Robert Ramey Terence Wilson wrote:
OK, that did it, but upon examination of the xml output I noticed the base class is not being serialized. A breakpoint inserted in CMyBaseClass::serialize never gets hit. Any thoughts?
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Wednesday, January 04, 2006 8:09 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [serialization] assert when serializingpolymorphictype to XML (Visual Studio 8)
Note that for "export" to work, the boost/serialization/export.hpp header must follow any archive headers. So moving the xml_iarchive.hpp .. up to where the text_iarchive.hpp headers are included should address the problem.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Thursday, January 05, 2006 7:58 AM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [serialization] assertwhenserializingpolymorphictype to XML (Visual Studio 8)
Look in the documentation under "base_object"
Robert Ramey
I made a dumb mistake: instead of ar & BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyClassBase); I had BOOST_SERIALIZATION_BASE_OBJECT_NVP(CMyClassBase); It's just one of those things it took another pair of eyes to notice. Thanks any way.
Terence Wilson wrote:
OK, that did it, but upon examination of the xml output I noticed the base class is not being serialized. A breakpoint inserted in CMyBaseClass::serialize never gets hit. Any thoughts?
-----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users- bounces@lists.boost.org] On Behalf Of Robert Ramey Sent: Wednesday, January 04, 2006 8:09 PM To: boost-users@lists.boost.org Subject: Re: [Boost-users] [serialization] assert when serializingpolymorphictype to XML (Visual Studio 8)
Note that for "export" to work, the boost/serialization/export.hpp header must follow any archive headers. So moving the xml_iarchive.hpp .. up to where the text_iarchive.hpp headers are included should address the problem.
Robert Ramey
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
_______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
participants (2)
-
Robert Ramey
-
Terence Wilson