c++ - QwtPlot - 堆损坏检测

标签 c++ qt qwt

我的代码中有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/

相关文章:

c++ - QDomNode.clear() 不会删除 xml 标记的值

c++ - QwtPlotSpectrogram 不起作用(无 ImageMode 绘图)

QT_STATIC_CONST 没有命名类型

c++ - 将未知数量的由空格分隔的整数读入每行一个 vector

c++ - 如何搜索boost文档?

python - 将 python 脚本输出重定向到 grep

c++ - Qt 单元测试 : why "qmake" is not recognized

css - QComboBox 样式表左边距没有副作用

c++ - 模板问题

c++ - 无法使用 Qt 生成具有可接受输出质量的 pdf