使用boost保存数据时赛格故障::系列化 [英] Seg fault when saving data using boost::serialization

查看:121
本文介绍了使用boost保存数据时赛格故障::系列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试使用升压序列化保存数据库,我遇到我解决不了的段错误。请你帮忙吗?任何建议greately AP preciated。
我下面牵制声明的罪魁祸首,但不知道如何解决它。

  OA<< *这个;

我的code部分如下:

 模板<类归档和GT;
无效NDB ::连载(归档和放大器; AR,const的无符号整型版){
    AR&安培; _layers;
    AR&安培; _MACROS;
    AR&安培; _MODULES;
    AR&安培; _INSTS;
}无效NDB :: SAVE_DB(字符串文件名){
    的std :: OFS的ofstream(filename.c_str()的std :: IOS ::出来|的std :: IOS ::二进制);
    //断言(ofs.good());
    提高::档案:: binary_oarchive OA(OFS);
    OA<< *这个;
    ofs.close();
}


我能问题针点在我的数据库中的循环引用。在code是如下:

 模板<类归档和GT;
    空针::连载(归档和放大器; AR,const的无符号整型版){
    AR&安培; pin_Port;
    AR&安培; pin_Layer;
    }

 模板<类归档和GT;
    空口::连载(归档和放大器; AR,const的无符号整型版){
    AR&安培; port_Name;
    AR&安培; port_Use;
    AR&安培; port_Dir;
    AR&安培; port_PINS;
    }

下面

嗨是我的类定义

 类针{
    上市:
    端口* pin_Port;
    层* pin_Layer;
    // RECT * pin_shape;    销();
    〜销();    无效set_port(端口*);
    无效set_layer(层*);
    串GET_NAME();
    端口* get_port();
    层* get_layer();
    串get_layer_name();
    双get_layer_width();
    私人的:
    友元类的boost ::系列化::访问;
    模板<类归档和GT;
    无效连载(归档和放大器; AR,const的无符号整型版);
    };
    类口岸{
    上市:
    串port_Name;
    焦炭port_Use;
    焦炭port_Dir;
    pin_vector port_PINS;
    港口();
    〜端口();    无效set_name(字符串);
    无效set_use(INT);
    无效set_dir(INT);
    字符串GET_NAME(无效);
    字符串get_use(无效);
    字符串get_dir(无效);
    无效add_pin(PIN *);
    pin_vector get_all_pins(无效);    私人的:
    友元类的boost ::系列化::访问;
    模板<类归档和GT;
    无效连载(归档和放大器; AR,const的无符号整型版);
    };


解决方案

我怀疑罪魁祸首是 ofs.close(); 。在 binary_oarchive 对象需要走出去的范围的的底层流被关闭,因为 binary_oarchive 对象尝试写剩余的数据,并在其析构函数刷新流。

标准流被刷新,当他们走出去的范围自动关闭,所以该行是完全没有必要的(在这种情况下,对人体有害)。删除它,你应该是好去。

(无关,但 _layers _MACROS 等都是可怕的名字,而事实上在用户code非法的。摆脱无论是下划线或大写字母,preferably都有。)

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.)

这篇关于使用boost保存数据时赛格故障::系列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆