Question

I've got geap corruption detection in my code. An Error occure after destruction. The error is connected with QwtLegend and QwtPlotCurve pointers. I tried to use auto_ptr to have 100% certainty that memery is deallocated correctly, but even thought the error occures. I think that this is also connected with action where I pass these pointers to QwtPlot. Anyone can explain me how it should be correctly implemented? Below SSCCE code:

#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
Was it helpful?

Solution

I suspect that there is a double deletion of the same object (QwtLegend) occurs in your code:

  • Due to using auto_ptr in the Plot class,

  • I suspect that Qwt also deletes the legend pointer that use assigned to the plot with this->insertLegend(legend.get(), QwtPlot::BottomLegend); call. Just looking into QwtPlot sources, makes this obvious:

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

    And the private data uses QPointer to delete referenced legend:

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

So, I conclude that you do not need to explicitly delete your legend, bur rely on the fact that QwtPlot takes the ownership on it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top