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