我的代码中有geap损坏检测。销毁后发生错误。该错误与 QwtLegend 和 QwtPlotCurve 指针有关。我尝试使用 auto_ptr 来 100% 确定内存已正确释放,但即使认为发生了错误。我认为这也与我将这些指针传递给 QwtPlot 的操作有关。任何人都可以解释我应该如何正确实现?以下 SSCCE 代码:
#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
最佳答案
我怀疑您的代码中出现了对同一对象(QwtLegend)的双重删除:
由于在
Plot
中使用了auto_ptr类,我怀疑 Qwt 还删除了使用
this->insertLegend(legend.get(), QwtPlot::BottomLegend);
分配给绘图的图例指针。称呼。只需查看 QwtPlot 源代码,就可以清楚地看出这一点:QwtPlot::~QwtPlot() { [..] delete d_data; // <- deletes the private data }
私有(private)数据使用
QPointer
删除引用的图例:class QwtPlot::PrivateData { public: [..] QPointer<QwtAbstractLegend> legend; // <-- will delete the legend };
因此,我得出结论,您不需要显式删除 legend
,但依赖于 QwtPlot 拥有其所有权的事实。
关于c++ - QwtPlot - 堆损坏检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808327/