[Serialization]Cannot serialize pointer -- newbie needs help
I cannot serialize an object through a pointer.When running the program,it reports: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Below is the code : ////////////////////////////////////////////////////////////////////////////////////////////// // // static link library: libboost_serialization-vc71-s-1_33_1.lib // ////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <fstream> #include <list> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> using std::cout ; using std::endl ; class gps_position { public: int m_degrees ; int m_minu float m_seconds ; gps_position() { m_degrees = m_minutes = m_seconds = 0 ; } gps_position( int degrees, int minutes, float seconds ) { m_degrees = degrees ; m_minutes = minutes ; m_seconds = seconds ; } }; std::ostream& operator << ( std::ostream &out, const gps_position &g ) { return out << g.m_degrees << '\'' << g.m_minutes << '\'' << g.m_seconds; } namespace boost{ namespace serialization{ template <class Archive> void serialize( Archive &ar, gps_position &g, const unsigned int version ) { ar & g.m_degrees ; ar & g.m_minutes ; ar & g.m_seconds ; } } } class bus_stop { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & latitude; ar & longitude; } gps_position latitude; gps_position longitude; protected: bus_stop(const gps_position & lat_, const gps_position & long_) : latitude(lat_), longitude(long_) {} public: bus_stop(){} // See item # 14 in Effective C++ by Scott Meyers. // re non-virtual destructors in base classes. virtual ~bus_stop(){} friend std::ostream& operator << ( std::ostream &out, const bus_stop &bs ) ; }; std::ostream& operator << ( std::ostream &out, const bus_stop &bs ) { return out << bs.latitude << '\t' << bs.longitude ; } class bus_stop_corner : public bus_stop { friend class boost::serialization::access; std::string street1; std::string street2; virtual std::string description() const { return street1 + " and " + street2; } template<class Archive> void serialize(Archive &ar, const unsigned int version) { // save/load base class information ar & boost::serialization::base_object<bus_stop>(*this); ar & street1 & street2; } public: bus_stop_corner(){} bus_stop_corner(const gps_position & _lat, const gps_position & _long, const std::string & _s1, const std::string & _s2 ) : bus_stop(_lat, _long), street1(_s1), street2(_s2) { } friend std::ostream& operator << ( std::ostream &out, const bus_stop_corner &bsc ) ; }; std::ostream& operator << ( std::ostream &out, const bus_stop_corner &bsc ) { return out << dynamic_cast<const bus_stop&>(bsc) << ' ' << bsc.description() ; } class bus_route { friend class boost::serialization::access; bus_stop * stops[2]; template<class Archive> void serialize(Archive & ar, const unsigned int version) { int i; for(i = 0; i < 2; ++i) ar & stops[i]; } public: bus_route() { stops[0] = stops[1] = 0 ; } bus_route( bus_stop *bs1, bus_stop *bs2 ) { stops[0] = bs1, stops[1] = bs2 ; } }; int main() { const char *file_name = "non_intrusive.dat" ; bus_stop *bs0 = new bus_stop_corner( gps_position(34, 135, 52.560f), gps_position(134, 22, 78.30f), "24th Street", "10th Avenue" ); bus_stop *bs1 = new bus_stop_corner( gps_position(35, 137, 23.456f), gps_position(133, 35, 54.12f), "State street", "Cathedral Vista Lane" ); const bus_route br( bs0, bs1 ) ; { std::ofstream ofs( file_name ) ; boost::archive::text_oarchive oa( ofs ) ; oa << (br) ; } bus_route newbr ; { std::ifstream ifs( file_name ) ; boost::archive::text_iarchive ia( ifs ) ; ia >> newbr ; } return 0 ; } I think maybe it's because I didn't build the serialize library correctly.Ibuilded it with bjam.When debuging it with VS2003,an exception was thrown in the _heap_alloc_base (size_t) function at line 211: line 211: return HeapAlloc(_crtheap, 0, size); Function "void * __cdecl _heap_alloc_base (size_t size)" is in malloc.c.
Does the demo included with the library work? Robert Ramey "wei niu" <wei.niu@gmail.com> wrote in message news:58418a380708240143g4993867er7fa68758127f4c5a@mail.gmail.com... I cannot serialize an object through a pointer.When running the program,it reports: This application has requested the Runtime to terminate it in an unusual way. Please contact the application's support team for more information. Below is the code : ////////////////////////////////////////////////////////////////////////////////////////////// // // static link library: libboost_serialization-vc71-s-1_33_1.lib // ////////////////////////////////////////////////////////////////////////////////////////////// #include <iostream> #include <fstream> #include <list> #include <boost/archive/text_oarchive.hpp> #include <boost/archive/text_iarchive.hpp> #include <boost/serialization/base_object.hpp> #include <boost/serialization/utility.hpp> #include <boost/serialization/list.hpp> using std::cout ; using std::endl ; class gps_position { public: int m_degrees ; int m_minu float m_seconds ; gps_position() { m_degrees = m_minutes = m_seconds = 0 ; } gps_position( int degrees, int minutes, float seconds ) { m_degrees = degrees ; m_minutes = minutes ; m_seconds = seconds ; } }; std::ostream& operator << ( std::ostream &out, const gps_position &g ) { return out << g.m_degrees << '\'' << g.m_minutes << '\'' << g.m_seconds ; } namespace boost{ namespace serialization{ template <class Archive> void serialize( Archive &ar, gps_position &g, const unsigned int version ) { ar & g.m_degrees ; ar & g.m_minutes ; ar & g.m_seconds ; } } } class bus_stop { friend class boost::serialization::access; template<class Archive> void serialize(Archive & ar, const unsigned int version) { ar & latitude; ar & longitude; } gps_position latitude; gps_position longitude; protected: bus_stop(const gps_position & lat_, const gps_position & long_) : latitude(lat_), longitude(long_) {} public: bus_stop(){} // See item # 14 in Effective C++ by Scott Meyers. // re non-virtual destructors in base classes. virtual ~bus_stop(){} friend std::ostream& operator << ( std::ostream &out, const bus_stop &bs ) ; }; std::ostream& operator << ( std::ostream &out, const bus_stop &bs ) { return out << bs.latitude << '\t' << bs.longitude ; } class bus_stop_corner : public bus_stop { friend class boost::serialization::access; std::string street1; std::string street2; virtual std::string description() const { return street1 + " and " + street2; } template<class Archive> void serialize(Archive &ar, const unsigned int version) { // save/load base class information ar & boost::serialization::base_object<bus_stop>(*this); ar & street1 & street2; } public: bus_stop_corner(){} bus_stop_corner(const gps_position & _lat, const gps_position & _long, const std::string & _s1, const std::string & _s2 ) : bus_stop(_lat, _long), street1(_s1), street2(_s2) { } friend std::ostream& operator << ( std::ostream &out, const bus_stop_corner &bsc ) ; }; std::ostream& operator << ( std::ostream &out, const bus_stop_corner &bsc ) { return out << dynamic_cast<const bus_stop&>(bsc) << ' ' << bsc.description() ; } class bus_route { friend class boost::serialization::access; bus_stop * stops[2]; template<class Archive> void serialize(Archive & ar, const unsigned int version) { int i; for(i = 0; i < 2; ++i) ar & stops[i]; } public: bus_route() { stops[0] = stops[1] = 0 ; } bus_route( bus_stop *bs1, bus_stop *bs2 ) { stops[0] = bs1, stops[1] = bs2 ; } }; int main() { const char *file_name = "non_intrusive.dat" ; bus_stop *bs0 = new bus_stop_corner( gps_position(34, 135, 52.560f), gps_position(134, 22, 78.30f), "24th Street", "10th Avenue" ); bus_stop *bs1 = new bus_stop_corner( gps_position(35, 137, 23.456f), gps_position(133, 35, 54.12f), "State street", "Cathedral Vista Lane" ); const bus_route br( bs0, bs1 ) ; { std::ofstream ofs( file_name ) ; boost::archive::text_oarchive oa( ofs ) ; oa << (br) ; } bus_route newbr ; { std::ifstream ifs( file_name ) ; boost::archive::text_iarchive ia( ifs ) ; ia >> newbr ; } return 0 ; } I think maybe it's because I didn't build the serialize library correctly.I builded it with bjam.When debuging it with VS2003,an exception was thrown in the _heap_alloc_base (size_t) function at line 211: line 211: return HeapAlloc(_crtheap, 0, size); Function "void * __cdecl _heap_alloc_base (size_t size)" is in malloc.c. ------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Well, then there's a problem. Ideally you should run the whole serialization regression test suite which would isolate what part of the system is failing. Or better yet, since (if I remember correctly) you're using boost on a "new" platform (one not usually tested for boost) the best would be to run the whole boost test suite. I realise that this take a while. But it's the only way you can be sure you're on solid ground and pinpoint any failings which might be rippling through to some higher level tests in a non-obvious way. Robert Ramey "wei niu" <wei.niu@gmail.com> wrote in message news:58418a380708250118i6c274655pd86162b8e1d54df2@mail.gmail.com... 2007/8/24, Robert Ramey <ramey@rrsd.com>:
Does the demo included with the library work? No.And the code example is revised from demo.cpp.
------------------------------------------------------------------------------ _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
2007/8/25, Robert Ramey <ramey@rrsd.com>:
Well, then there's a problem.
Ideally you should run the whole serialization regression test suite which would isolate what part of the system is failing. Or better yet, since (if I remember correctly) you're using boost on a "new" platform (one not usually tested for boost) the best would be to run the whole boost test suite. I realise that this take a while. But it's the only way you can be sure you're on solid ground and pinpoint any failings which might be rippling through to some higher level tests in a non-obvious way.
I haven't noticed who you are until reading the documents of serialization library.Thank you,Robert. Sorry,I even don't know how to build the whole serialization regression test suite. Cannot serialize a pointer with boost is not a big problem,I can do it myself.But I want to know how boost do it.Unfortunately,for me boost's source code is very hard.Maybe a chart of the class hierarchy is helpful.Iever read SGI STL's list's source code,I think its easier than serialization library,and it's easy to print as it's in a single file. How did you design the serialization library? If you don't mind I can send my libboost_serialization-vc71-s-1_33_1.lib to you,then you may find where the problem is.My OS is win2000 sp4.My compiler is vc7.1. You must be very busy,so I'm afraid I'm wasting your time.I'll read your codes,hope I can understand them. Sincerely, Wei Niu
participants (2)
-
Robert Ramey
-
wei niu