
Hello everybody, I defined the following objects to be serialized: class MyElementObject { friend class boost::serialization::access; public: template<class Archive> void serialize(Archive & ar, const unsigned int version) { } }; template<class T> class MyRecursiveObject { friend class boost::serialization::access; public: T element; std::vector<MyRecursiveObject<T> > children; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & element; ar & children; } }; I then run the following code: int main() { //MyRecursiveObject initialization MyRecursiveObject<MyElementObject> rec_object; rec_object.children.push_back(MyRecursiveObject<MyElementObject>()); rec_object.children[0].children.push_back(MyRecursiveObject<MyElementObject>()); //create vector of pointers to MyRecursiveObject's elements vector<MyElementObject *> elt_ptrs; elt_ptrs.push_back(&rec_object.element); elt_ptrs.push_back(&rec_object.children[0].element); elt_ptrs.push_back(&rec_object.children[0].children[0].element); //serialize MyRecursiveObject and the vector of pointers { ofstream ofs("filename"); boost::archive::text_oarchive oa(ofs); oa << rec_object; oa << elt_ptrs; } //create new MyRecursiveObject and vector of pointers for deserialization MyRecursiveObject<MyElementObject> rec_object_deserialized; rec_object_deserialized.children.push_back(MyRecursiveObject<MyElementObject>()); rec_object_deserialized.children[0].children.push_back(MyRecursiveObject<MyElementObject>()); vector<MyElementObject *> elt_ptrs_deserialized; //deserialize { ifstream ifs("filename"); boost::archive::text_iarchive ia(ifs); ia >> rec_object_deserialized; ia >> elt_ptrs_deserialized; } //compare deserialized pointers cout<<"elt_ptrs first level="<<elt_ptrs_deserialized[0] <<" expected="<<&rec_object_deserialized.element<<endl; cout<<"elt_ptrs second level="<<elt_ptrs_deserialized[1] <<" expected="<<&rec_object_deserialized.children[0].element<<endl; cout<<"elt_ptrs third level="<<elt_ptrs_deserialized[2] <<" expected="<<&rec_object_deserialized.children[0].children[0].element<<endl; return 0; } And I always get an output similar to the following one: elt_ptrs first level=<b>0x7fff57c787c0* expected=*0x7fff57c787c0* elt_ptrs second level=*0x18e7020* expected=*0x18e7020* elt_ptrs third level=*0xffff8000ab5564f0* expected=*0x18e7450* As can be seen from the output I manage to deserialize pointers that point to elements down to the second recursion level of MyRecursiveObject. As soon as I try to do it with pointers to the third level or even deeper the deserialization fails. Am I using boost::serialization wrongly? Note that the MyRecursiveObject is always successfully deserialized, no matter how many levels it has. I encounter the problem only deserializing pointers to its elements. Thank you in advance Kean -- View this message in context: http://boost.2283326.n4.nabble.com/serialization-of-recursive-objects-tp4639... Sent from the Boost - Users mailing list archive at Nabble.com.

-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 Hi! Am 27.11.12 11:13, schrieb kean:
//serialize MyRecursiveObject and the vector of pointers { ofstream ofs("filename"); boost::archive::text_oarchive oa(ofs); oa << rec_object; oa << elt_ptrs; }
The point here is object tracking: serializing a pointer will invoke object tracking, but your pointer points to an object that has already been serialized without tracking. You will need to put the following into your code: BOOST_CLASS_TRACKING( MyElementObject, boost::serialization::track_always) Docs are at http://www.boost.org/doc/libs/1_51_0/libs/serialization/doc/traits.html#trac... Frank -----BEGIN PGP SIGNATURE----- Version: GnuPG/MacGPG2 v2.0.17 (Darwin) Comment: GPGTools - http://gpgtools.org Comment: keyserver x-hkp://pool.sks-keyservers.net iEYEARECAAYFAlC3k+cACgkQhAOUmAZhnmr1twCfcfdDVX9LQENB2gQO3E1MLk/V 7/oAnRdJYQm0q0xOtFXZ7vXVkqLLA+HT =GdiJ -----END PGP SIGNATURE-----
participants (2)
-
Frank Birbacher
-
kean