在析构函数调用

时间:2015-12-15 13:10:33

标签: c++ c++11 shared-ptr unique-ptr upcasting

我有当前的课程

class VectorVectorRow : public virtual RowFormat {

private:
    std::vector<std::vector<double>>* mat;
    int r_pos;

public:
    VectorVectorRow  (std::vector<std::vector<double>>* r,int pos) : mat{r}, r_pos{pos} {};
    VectorVectorRow (const VectorVectorRow& a) : mat(a.mat), r_pos{a.r_pos} {}

    //Where the SIGABRT exception is risen
    virtual ~VectorVectorRow()  = default; // inline;
    virtual double getCell(int pos);
    virtual int getSize();
    virtual void setCell(int pos, double value);
};

继承了以下虚拟类:

class RowFormat { public:

    RowFormat() {};
    /**
     * @param pos   returns the cell at the current position
     */
    virtual double getCell(int pos)  = 0;

    /** returns the size of the row
    */
    virtual int getSize()  = 0;

    /**
     * @param pos       position of the cell
     * @param value     value that has to be stored
     */
    virtual void setCell(int pos, double value)  = 0;

    void increment(int pos, double value) {
        setCell(pos,getCell(pos)+value);
    }

    virtual ~RowFormat() =default; };

第一个类的实例按以下方式使用:

for(int epoch=0; epoch < training_epochs; epoch++) {
    for (int row=0; row < train_N; row++) {
        unique_ptr<RowFormat> k{std::move(df->getRow(row))}; //getRow returns an unique_ptr of VectorVectorRow up-casted to RowFormat
        da.train(k.get(),learning_rate,corruption_level);
        // just before the block close, a SIGABRT exception is risen
    }
}

getRow的特定实例的df定义如下:

// std::shared_ptr<std::vector<std::vector<double>>> mat;
std::unique_ptr<RowFormat> VectorVectorData::getRow(int pos) {
    std::unique_ptr<VectorVectorRow> ptr{new VectorVectorRow(mat.get(),pos)};
    std::unique_ptr<RowFormat> casted{dynamic_cast<RowFormat*>(ptr.get())};
    return casted;
}

完整的堆栈调用如下: stack call depiction

我甚至尝试用std :: shared_ptr替换std::vector<std::vector<double>>* mat;,但错误仍然存​​在。我怎么能避免在块结束时的执行?提前谢谢。

0 个答案:

没有答案