简体   繁体   中英

Seg fault when saving data using boost::serialization

When trying to save the database using boost serialization, I encounter the segfault that I could not resolve. Would you please help? Any suggestion is greately appreciated. I've pinned down the statement below as the culprit but not sure of how to resolve it

oa << *this;

My code section is below:

template<class Archive>
void nDB::serialize(Archive &ar, const unsigned int version) {
    ar & _LAYERS;
    ar & _MACROS;
    ar & _MODULES;
    ar & _INSTS;
}

void nDB::save_db(string filename) {
    std::ofstream ofs(filename.c_str(), std::ios::out | std::ios::binary);
    //assert(ofs.good());
    boost::archive::binary_oarchive oa(ofs);
    oa << *this;
    ofs.close();
}

Hi, I was able to pin point the issue to a circular reference in my database. The code are below:

    template<class Archive>
    void pin::serialize(Archive &ar, const unsigned int version) {
    ar & pin_Port;
    ar & pin_Layer;
    }

and

    template<class Archive>
    void port::serialize(Archive & ar, const unsigned int version){
    ar & port_Name;
    ar & port_Use;
    ar & port_Dir;
    ar & port_PINS;
    }

Hi below are my class definition

    class pin {
    public:
    port*       pin_Port;
    layer*      pin_Layer;
    // rect*       pin_shape;

    pin();
    ~pin();

    void         set_port(port*);
    void         set_layer(layer*);
    string       get_name();
    port*        get_port();
    layer*       get_layer();
    string       get_layer_name();
    double       get_layer_width();
    private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version);
    };
    class port {
    public:
    string     port_Name;
    char       port_Use;
    char       port_Dir;
    pin_vector port_PINS;
    port();
    ~port();

    void         set_name(string);
    void         set_use(int);
    void         set_dir(int);
    string       get_name(void);
    string       get_use(void);
    string       get_dir(void);
    void         add_pin(pin*);
    pin_vector   get_all_pins(void);

    private:
    friend class boost::serialization::access;
    template<class Archive>
    void serialize(Archive & ar, const unsigned int version);
    };

I suspect the culprit is ofs.close(); . The binary_oarchive object needs to go out of scope before the underlying stream is closed, because the binary_oarchive object tries to write remaining data and flush the stream in its destructor.

Standard streams are flushed and closed automatically when they go out of scope, so the line is completely unnecessary (and in this case, harmful). Remove it and you should be good to go.

(Unrelated, but _LAYERS , _MACROS , etc. are terrible names, and in fact are illegal in user code. Get rid of either the leading underscore or the capital letters, preferably both.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM