[Serialisation] Question on shared_ptr's and base/derived classes

Hi All, I am trying to serialise a vector of shared_ptr's which point to a base class which may be one of a number of derived class (Not polymorphic in that the base class has no virtual functions - its just supplies base functionality to derived classes) In very pseudo code Class A Class B : public A Class C : public A vector<shared_ptr<A> > list; list.insert(shared_ptr<A> (new B)); list.insert(shared_ptr<A> (new C)); In serialise Ar & boost::serialization::make_nvp("ListofStuff", list); Reading the documentation, I believe this should work, but I am getting a bus error on our embedded system when it tries to serialise the vector (all the other bits have serialised ok which implies it's all compiled OK) - it managed "<px" into the XML archive then failed (see below for file). Bus error can mean a number of things on our board, but usually an alignment problem. Anyone any idea where I should look to sort this out? XML output (starts to serialise pointer on last line): <?xml version="1.0" encoding="UTF-8" standalone="yes" ?> <!DOCTYPE boost_serialization> <boost_serialization signature="serialization::archive" version="3"> <temp class_id="0" tracking_level="0" version="0"> <Name>Message1</Name> <Filename class_id="1" tracking_level="0" version="0"> <path>/home/jhughes/msg.xml</path> </Filename> <Panels class_id="2" tracking_level="0" version="0"> <count>1</count> <item class_id="3" tracking_level="0" version="1"> <px</boost_serialization> James This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.

One comment is that: Class A { }; // No virtual destructor Class B : public A { } shared_ptr<A>(new B) Should cause UB as the shared pointer will will eventually call delete on its parameter, and since A has no virtual destructor and is NOT the type the object was created with, you get UB. It basically boils down to the equivalent to this A* ptr = new B; delete ptr; which is a no-no. A NEEDS to have a virtual destructor to use it the way you are. Richard

Richard Damon wrote:
Hi Richard, actually it's ok with shared_ptr. Here is a short snip from the docs: ... This constructor has been changed to a template in order to remember the actual pointer type passed. The destructor will call delete with the same pointer, complete with its original type, even when T does not have a virtual destructor, or is void. ...

I checked, the base did not have a virtual destructor (I didn't write it!!), but even adding that, the code still bus errors. Is there anything specific I need to add for serialisation of shared_ptr and derived classes? Currently I just have the serialise functions in all the classes being serialised. I already call the base class serialisation using base_obect in the derived classes serialize functions, but I haven't added the BOOST_CLASS_EXPORT_GUID stuff referred to in certain parts of the docs as I thought that it should work without it - reason being it doesn't throw unregistered_class, it bus errors. Strangely, I have now tried to use the BOOST_CLASS_EXPORT_GUID macros in the derived class, and they cause a compile fault along the lines of... BOOST_CLASS_EXPORT_GUID(LCTextPanel, "TextPanel"); causes
So if anyone knows what that means as it might be what's causing the other issue....?? James -----Original Message----- From: boost-users-bounces@lists.boost.org [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Richard Damon Sent: 02 May 2007 12:19 To: boost-users@lists.boost.org Subject: Re: [Boost-users] [Serialisation] Question on shared_ptr'sand base/derived classes One comment is that: Class A { }; // No virtual destructor Class B : public A { } shared_ptr<A>(new B) Should cause UB as the shared pointer will will eventually call delete on its parameter, and since A has no virtual destructor and is NOT the type the object was created with, you get UB. It basically boils down to the equivalent to this A* ptr = new B; delete ptr; which is a no-no. A NEEDS to have a virtual destructor to use it the way you are. Richard _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.

Hughes, James wrote:
I don't think this is generally possible. The default extended type info relies on dynamic cast for downcasting. Try making at least one function in the base class virtual. Robert Ramey

> -----Original Message----- > From: boost-users-bounces@lists.boost.org > [mailto:boost-users-bounces@lists.boost.org] On Behalf Of Robert Ramey > Sent: 02 May 2007 14:08 > To: boost-users@lists.boost.org > Subject: Re: [Boost-users] [Serialisation] Question on > shared_ptr'sandbase/derived classes > > Hughes, James wrote: > > Hi All, > > > > I am trying to serialise a vector of shared_ptr's which point to a > > base class which may be one of a number of derived class (Not > > polymorphic in that the base class has no virtual functions > - its just > > supplies base functionality to derived classes) > > I don't think this is generally possible. > > The default extended type info relies on dynamic cast for downcasting. > > Try making at least one function in the base class virtual. Hello Robert, I just had a look through the class, and it does have some virtual functions so that doesn't appear to be the problem. Would just having a virtual destructor be good enough anyway? > > Robert Ramey > > > > _______________________________________________ > Boost-users mailing list > Boost-users@lists.boost.org > http://lists.boost.org/mailman/listinfo.cgi/boost-users > This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.

I have now fixed this problem - I needed to add BOOST_CLASS_EXPORT_GUID code to my derived classes and it all started working. There was also an issue with NVP around a base class which required BOOST_SERIALIZATION_BASE_OBJECT_NVP. Thanks to all for the help. Rgds James > -----Original Message----- > From: boost-users-bounces@lists.boost.org > [mailto:boost-users-bounces@lists.boost.org] On Behalf Of > Hughes, James > Sent: 02 May 2007 14:18 > To: boost-users@lists.boost.org > Subject: Re: [Boost-users] [Serialisation] Question > onshared_ptr'sandbase/derived classes > > > > -----Original Message----- > > From: boost-users-bounces@lists.boost.org > > [mailto:boost-users-bounces@lists.boost.org] On Behalf Of > Robert Ramey > > Sent: 02 May 2007 14:08 > > To: boost-users@lists.boost.org > > Subject: Re: [Boost-users] [Serialisation] Question on > > shared_ptr'sandbase/derived classes > > > > Hughes, James wrote: > > > Hi All, > > > > > > I am trying to serialise a vector of shared_ptr's which > point to a > > > base class which may be one of a number of derived class (Not > > > polymorphic in that the base class has no virtual functions > > - its just > > > supplies base functionality to derived classes) > > > > I don't think this is generally possible. > > > > The default extended type info relies on dynamic cast for > downcasting. > > > > Try making at least one function in the base class virtual. > > Hello Robert, > > I just had a look through the class, and it does have some > virtual functions so that doesn't appear to be the problem. > Would just having a virtual destructor be good enough anyway? > > > > > > Robert Ramey > > > > > > > > _______________________________________________ > > Boost-users mailing list > > Boost-users@lists.boost.org > > http://lists.boost.org/mailman/listinfo.cgi/boost-users > > > > This message (including any attachments) contains > confidential and/or proprietary information intended only for > the addressee. > Any unauthorized disclosure, copying, distribution or > reliance on the contents of this information is strictly > prohibited and may constitute a violation of law. If you are > not the intended recipient, please notify the sender > immediately by responding to this e-mail, and delete the > message from your system. If you have any questions about > this e-mail please notify the sender immediately. > _______________________________________________ > Boost-users mailing list > Boost-users@lists.boost.org > http://lists.boost.org/mailman/listinfo.cgi/boost-users > This message (including any attachments) contains confidential and/or proprietary information intended only for the addressee. Any unauthorized disclosure, copying, distribution or reliance on the contents of this information is strictly prohibited and may constitute a violation of law. If you are not the intended recipient, please notify the sender immediately by responding to this e-mail, and delete the message from your system. If you have any questions about this e-mail please notify the sender immediately.
participants (4)
-
David Klein
-
Hughes, James
-
Richard Damon
-
Robert Ramey