core dump while Object Serialization of list <const base * >

Discussion in 'C++' started by Pallav singh, Aug 20, 2011.

  1. Pallav singh

    Pallav singh Guest

    Hi All ,

    i am getting core dump while Object Serialization of list <const
    base * >

    ///////////////////////////////////////////////////////////////////////
    #include <iostream>

    /* Header file to Include FileStreams */
    #include <fstream>

    /* Header file to Include Archieving Files */
    #include <boost/archive/text_oarchive.hpp>
    #include <boost/archive/text_iarchive.hpp>

    /* Derived classes should include serializations of their base classes
    */
    #include <boost/serialization/base_object.hpp>

    /* List of containers to be serialized */
    #include <boost/serialization/list.hpp>

    /* To split Save() and Load() Function */
    #include <boost/serialization/split_member.hpp>

    ////////////////////////////////////////////////////////////////////////

    class gps_position
    {
    private:
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    ar & degrees;
    ar & minutes;
    ar & seconds;
    }

    int degrees;
    int minutes;
    float seconds;

    public:

    gps_position(){};
    gps_position(int d, int m, float s) :
    degrees(d), minutes(m), seconds(s)
    {}

    };

    ////////////////////////////////////////////////////////////////////////

    class bus_stop
    {
    /* to proivide ACCESS TO private member variable of class */
    friend class boost::serialization::access;

    /* Always keep Serilization funtions in Private part of class
    Else
    * Object Tracking will be creating Problem for Polymorphic class
    object
    */
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    ar & latitude;
    ar & longitude;
    }

    gps_position latitude;
    gps_position longitude;

    public:

    bus_stop(const gps_position & lat_, const gps_position & long_) :
    latitude(lat_), longitude(long_)
    {}

    bus_stop(){}

    virtual ~bus_stop(){}
    };

    ////////////////////////////////////////////////////////////////////////////

    class bus_stop_corner : public bus_stop
    {
    friend class boost::serialization::access;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    // serialize base class information
    ar & boost::serialization::base_object<bus_stop>(*this);
    ar & street1;
    ar & street2;
    }
    std::string street1;
    std::string street2;
    virtual std::string description() const
    {
    return street1 + " and " + 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_)
    {}
    };

    //////////////////////////////////////////////////////////////////////////

    class Save_Polymorphic_Objects
    {
    friend class boost::serialization::access;

    // Create List to serialize Polymorphic Objects
    std::list<const bus_stop *> stops;

    template<class Archive>
    void serialize(Archive & ar, const unsigned int version)
    {
    // note, version is always the latest when saving
    std::cout <<" Template Function Save Called \n";
    ar & stops;
    }

    public:
    Save_Polymorphic_Objects(){}

    void Insert_Polymorphic_Object(){
    const gps_position position_10 (10, 10, 10.567f);
    const gps_position position_20 (20, 20, 20.567f);

    const gps_position position_11 (11, 11, 11.567f);
    const gps_position position_21 (21, 21, 21.567f);

    const gps_position position_12 (12, 12, 12.567f);
    const gps_position position_22 (22, 22, 22.567f);

    const gps_position position_13 (13, 13, 13.567f);
    const gps_position position_23 (23, 23, 23.567f);

    const gps_position position_14 (14, 14, 14.567f);
    const gps_position position_24 (24, 24, 24.567f);

    const bus_stop_corner bus_stop_corner_1( position_10,
    position_20, "New Delhi_0", " Mumbai_0");
    const bus_stop_corner bus_stop_corner_2( position_11,
    position_21, "New Delhi_1", " Mumbai_1");
    const bus_stop_corner bus_stop_corner_3( position_12,
    position_22, "New Delhi_2", " Mumbai_2");

    const bus_stop bus_stop_1( position_13, position_23);
    const bus_stop bus_stop_2( position_14, position_24);

    stops.push_back( &bus_stop_corner_1);
    stops.push_back( &bus_stop_1);
    stops.push_back( &bus_stop_2);
    stops.push_back( &bus_stop_corner_2);
    stops.push_back( &bus_stop_corner_3);
    }
    };

    //////////////////////////////////////////////////////////////////////////

    int main() {

    std::eek:fstream ofs("msg_file_store");

    Save_Polymorphic_Objects polymorphic_object_1;
    polymorphic_object_1.Insert_Polymorphic_Object();

    {
    boost::archive::text_oarchive oa(ofs);
    oa << polymorphic_object_1;
    }

    // Create a Dummy Object Location where we need to retieve Data
    Save_Polymorphic_Objects polymorphic_object_2;

    {
    std::ifstream ifs("msg_file_store");
    boost::archive::text_iarchive ia(ifs);

    ia >> polymorphic_object_2;
    }

    return 0;
    }

    ////////////////////////////////////////////////////////////////////////

    Thanks
    Pallav Singh
    Pallav singh, Aug 20, 2011
    #1
    1. Advertising

  2. Re: core dump while Object Serialization of list <const base *>

    On 8/20/2011 6:31 AM, Pallav singh wrote:
    > i am getting core dump while Object Serialization of list<const
    > base *>
    > [..code..]


    Core dumps are usually caused by accessing the memory that doesn't
    belong to your process. That most likely happens when dereferencing
    pointers to objects that have already been deleted or pointers with
    random values (uninitialized). Debugging core dumps is usually not at
    all tricky, there is nothing really to write to a newsgroup about. Learn
    to use a debugger.

    V
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Aug 21, 2011
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. halfdog
    Replies:
    12
    Views:
    12,436
  2. Replies:
    8
    Views:
    404
    Keith Thompson
    Jan 10, 2006
  3. Javier
    Replies:
    2
    Views:
    559
    James Kanze
    Sep 4, 2007
  4. Pallav singh
    Replies:
    0
    Views:
    261
    Pallav singh
    Aug 20, 2011
  5. Sachin
    Replies:
    0
    Views:
    91
    Sachin
    Nov 9, 2004
Loading...

Share This Page