Boost logo

Boost Users :

From: Henrik Jacobsson (henrik.jacobsson_at_[hidden])
Date: 2005-05-11 03:37:29


Robert Ramey wrote:
> This should work - How is your case different that test_cycle.cpp ?

I can't find test_cycle.cpp unfortunately, so I don't know. See my example below.

> The pointer conflict means that something has been serializaed as first as a
> pointer then as an object. Normally this should be easily avoidable. This
> is flagged as an error because the de-serialization would be a problem.
> First the deserialization of a pointer creates an object - next the object
> itself is re-loaded - but now we have two objects when we started with one.

I see what you mean, but I fail how to see a convenient way to avoid it without
changing all containers so that they have pointers instead of objects or
something like that. Is perhaps BOOST_CLASS_TRACKING the key?

---
#include <vector>
#include <fstream>
#include <boost/serialization/vector.hpp>
#include <boost/archive/xml_oarchive.hpp>
class A {
   friend class boost::serialization::access;
public:
   A() : next(0) { };
   A* next;
private:
   template<class Archive>
   void serialize(Archive & ar, const unsigned int){
     ar & BOOST_SERIALIZATION_NVP(next);
   }
};
int
main()
{
   std::ofstream ofs("test_of_serialization.dat");
   boost::archive::xml_oarchive oa(ofs);
   std::vector<A> a1(4);
   a1[0].next = &a1[0];
   a1[1].next = &a1[1];
   a1[2].next = &a1[2];
   a1[3].next = &a1[3];
   oa << BOOST_SERIALIZATION_NVP(a1);
   // everything worked fine with a1...
   std::vector<A*> a2(4);
   a2[0] = new A;
   a2[1] = new A;
   a2[2] = new A;
   a2[3] = new A;
   a2[0]->next = a2[1];
   a2[1]->next = a2[2];
   a2[2]->next = a2[3];
   a2[3]->next = a2[0];
   oa << BOOST_SERIALIZATION_NVP(a2);
   // everything worked fine with a2...
   std::vector<A> a3(4);
   a3[0].next = &a3[1];
   a3[1].next = &a3[2];
   a3[2].next = &a3[3];
   a3[3].next = &a3[0];
   // but this throws "pointer conflict" exception
   oa << BOOST_SERIALIZATION_NVP(a3);
}
---
// Henrik Jacobsson
> Robert Ramey
> 
> Henrik Jacobsson wrote:
> 
>>Hi!
>>
>>I want to serialize objects that link to each other through pointers,
>>like a linked list, but in this case a more complex graph with
>>several different classes and proxies (a graph which can contain
>>loops and and null-pointers).
>>
>>But I get the "pointer conflict" exception since the serialization
>>discovers that an object have been serialized once before through its
>>pointer. The contents should be serialized/deserialized only once,
>>and then the pointers should just be kept consistent.
>>
>>An alternative is of course to replace all the pointers with indeces
>>which are memory-independent. But that's a messy affair.
>>
>>In the copy constructor I solved something similar through a
>>map<Type*,Type*> which mapped from the old pointers to the newly
>>allocated ones, so that they could be properly replaced. Is something
>>like this hidden somewhere in the lib?
>>
>>Any suggestions?
>>
>>Best regards,
>>Henrik Jacobsson
> 
> 
> 
> 
> _______________________________________________
> 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