serialization of derived classes...
Hi list, I'm using the boost::serialization lib to archive classes in a VC7.1 program. I have a class C, derived from B, derived from (abstract) A now, in serialisation of C, I did: ar & boost::serialization::make_nvp( "baseobject", boost::serialization::base_object<B>( t ) ); This seems to give a memory leak on closing the application. So I tried the following 2 lines: ar & boost::serialization::make_nvp( "baseobject", boost::serialization::base_object<A>( t ) ); ar & boost::serialization::make_nvp( "baseobject2", boost::serialization::base_object<B>( t ) ); and this seemed to work.... What is the correct way of saving C (derived from B which is derived from A)? It seems a bit strange to me that i have to call serialization of A in the ser. of C. Shouldn't B take care of this? thanks, Filip Peters __________________________________ Yahoo! Mail Stay connected, organized, and protected. Take the tour: http://tour.mail.yahoo.com/mailtour.html
There is a section of the manual which describes the issues related to serialization of derived pointers through a pointer to he the base class. When serializing pointers, its very easy to create memory leaks. The loading of pointers creates new objects. If the data structures are such that these objects have no owner, you'll probably get leaks. To my dismay, I found that lots of my tests have leaks that I had overlooked before the test system started to check for them. They don't affect the tests themselves but the lesson is pretty clear. Look into using shared_ptr when classes contain pointers. Robert Ramey Filip Peters wrote:
Hi list,
I'm using the boost::serialization lib to archive classes in a VC7.1 program. I have a class C, derived from B, derived from (abstract) A
now, in serialisation of C, I did: ar & boost::serialization::make_nvp( "baseobject", boost::serialization::base_object<B>( t ) );
This seems to give a memory leak on closing the application.
So I tried the following 2 lines: ar & boost::serialization::make_nvp( "baseobject", boost::serialization::base_object<A>( t ) ); ar & boost::serialization::make_nvp( "baseobject2", boost::serialization::base_object<B>( t ) );
and this seemed to work....
What is the correct way of saving C (derived from B which is derived from A)? It seems a bit strange to me that i have to call serialization of A in the ser. of C. Shouldn't B take care of this?
thanks, Filip Peters
__________________________________ Yahoo! Mail Stay connected, organized, and protected. Take the tour: http://tour.mail.yahoo.com/mailtour.html
participants (2)
-
Filip Peters
-
Robert Ramey