2014-04-02 51 views
2

我的代码中有geap腐败检测。销毁后发生错误。该错误与QwtLegend和QwtPlotCurve指针相关联。我试图使用auto_ptr 100%确定memery被正确释放,但即使认为错误发生了。我认为这也与我将这些指针传递给QwtPlot的操作相关联。任何人都可以解释我应该如何正确实施?下面SSCCE代码:QwtPlot - 堆腐败检测

#include "plot.h" 

Plot::Plot(QWidget *parent) : 
    QwtPlot(parent) 
{ 
    setUpPlot(); 
    setUpCurves(); 
} 

    void Plot::setUpPlot() 
    { 
     legend = std::auto_ptr<QwtLegend>(new QwtLegend); 
     legend->setFrameStyle(QFrame::Box|QFrame::Sunken); 
     this->insertLegend(legend.get(), QwtPlot::BottomLegend); 
    } 

    void Plot::setUpCurves() 
    { 
     aXCurve = new QwtPlotCurve("Acceleration in X axis"); 
     aXCurve->attach(this); 
     replot(); 
    } 

Plot::~Plot() 
{ 
    aXCurve->detach(); 
    delete aXCurve; 
    aXCurve = NULL; 
} 

#ifndef PLOT_H 
#define PLOT_H 

#include <qwt_plot.h> 
#include <qwt_legend.h> 
#include <qwt_plot_curve.h> 

class Plot : public QwtPlot 
{ 
    Q_OBJECT 
public: 
    explicit Plot(QWidget *parent = 0); 
    ~Plot(); 

private: 
    void setUpPlot(); 
    void setUpCurves(); 

    std::auto_ptr<QwtLegend> legend; 
    QwtPlotCurve *aXCurve; 
}; 

#endif // PLOT_H 
+0

虽然这里不是bug的来源,但不要使用'std :: auto_ptr' - 它从根本上被破坏,并允许你做一些简单的错误来源(如赋值/复制)。你想要'std :: unique_ptr'或'QScopedPointer'。 –

回答

3

我怀疑有同一对象的双重缺失(QwtLegend)发生在你的代码:

  • 由于在Plot类使用的auto_ptr,

  • 我怀疑Qwt还会删除使用this->insertLegend(legend.get(), QwtPlot::BottomLegend);调用分配给该图的图例指针。只要寻找到QwtPlot来源,使这明显:

    QwtPlot::~QwtPlot() 
    { 
        [..] 
        delete d_data; // <- deletes the private data 
    } 
    

    而私人数据使用QPointer删除引用的传说:

    class QwtPlot::PrivateData 
    { 
    public: 
        [..] 
        QPointer<QwtAbstractLegend> legend; // <-- will delete the legend 
    }; 
    

所以,我的结论是,你并不需要显式删除您的legend,bur依赖于QwtPlot取得它的所有权的事实。