gpt4 book ai didi

c++ - QwtPlot - 堆损坏检测

转载 作者:搜寻专家 更新时间:2023-10-31 01:44:57 28 4
gpt4 key购买 nike

我的代码中有 geap 损坏检测。销毁后发生错误。该错误与 QwtLegend 和 QwtPlotCurve 指针有关。我尝试使用 auto_ptr 来 100% 确定 memery 被正确释放,但甚至认为错误发生了。我认为这也与我将这些指针传递给 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
    };

因此,我得出结论,您不需要显式删除您的图例,而是依赖于 QwtPlot 拥有它的所有权这一事实。

关于c++ - QwtPlot - 堆损坏检测,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22808327/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com