Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

有關釋放Qwtplot 造成程式崩潰



  • 作業系統 Ubuntu 18.04
    Qt Version : 5.9.5
    QMake Version: 3.1
    GCC 7.4.0

    各位前輩 大家好,
    我在Qt上面建立了多個Qwtplot的元件,每一個元件都在原點設定一個點,當我在結束程式前,在解構子裡~MainWindow()釋放時,程式就會崩潰。其程式碼如下:
    mainwindow.h

    #include <QMainWindow>
    #include <qwt_plot.h>
    #include <qwt_plot_curve.h>
    #include <qwt_plot_grid.h>
    #include <qwt_plot_canvas.h>
    #include <qwt_plot_curve.h>
    #include <qwt_plot_grid.h>
    #include <qwt_symbol.h>
    #include <QPolygonF>
    namespace Ui {
    class MainWindow;
    }
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    private:
        Ui::MainWindow *ui;
        QwtPlot *qwtpt;
        QPolygonF *polygon;
        QwtPlotCurve *curve;
        QwtPlotGrid *grid;
        QwtSymbol *symbol;
    };
    

    mainwindow.cpp

    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent),
        ui(new Ui::MainWindow)
    {
        ui->setupUi(this);
        int numpoints = 10;
        qwtpt = new QwtPlot[numpoints];
        polygon = new QPolygonF[numpoints];
        curve = new QwtPlotCurve[numpoints];
        grid = new QwtPlotGrid[numpoints];
        symbol = new QwtSymbol[numpoints];
        QString style = "color: white; ";
        QPolygonF initialpoint;
        initialpoint << QPointF(0.0f, 0.0f);
        int k;
        for(k = 0; k < numpoints; ++k){
            //plot
            qwtpt[k].setParent(ui->scrollArea);
            qwtpt[k].setGeometry(10, 20 + 181 * k, 760, 161);
            qwtpt[k].setAxisTitle(QwtPlot::xBottom, "points");
            qwtpt[k].setAxisTitle(QwtPlot::yLeft, "efficience %");
            qwtpt[k].setAxisScale(QwtPlot::xBottom, 0.0, 132);
            qwtpt[k].setAxisScale(QwtPlot::yLeft, 0.0, 100);
            qwtpt[k].setStyleSheet(style);
            //symbol
            symbol[k].setPen(QPen(Qt::red, 2));
            symbol[k].setStyle(QwtSymbol::Ellipse);
            symbol[k].setBrush(QBrush(Qt::yellow));
            symbol[k].setSize(QSize(8,8));
            //curve
            curve[k].setPen(Qt::blue, 4);
            curve[k].setRenderHint(QwtPlotItem::RenderAntialiased, true);
            curve[k].setSymbol(&symbol[k]);
            curve[k].attach(&qwtpt[k]);
            curve[k].setSamples(initialpoint);
            //grid
            grid[k].attach(&qwtpt[k]);
        }
    }
    MainWindow::~MainWindow()
    {
        delete [] qwtpt;	//  Qt Debug 顯示在這行崩潰的
        delete [] polygon;
        delete [] curve;
        delete [] grid;
        delete [] symbol;
        delete ui;
    }
    

    如果,我沒有進行釋放的動作,則Qt Debug會顯示在~MainWindow()崩潰

    若我將delete [] qwtpt;與delete [] curve;調換位置,則崩潰的點變成delete [] curve;這行

    MainWindow::~MainWindow()
    {
         delete [] curve;	//  Qt Debug 顯示在這行崩潰的
        delete [] polygon;
        delete [] qwtpt;
        delete [] grid;
        delete [] symbol;
        delete ui;
    }
    

    請問各位前輩是否有建議可以修正這個問題。
    感謝各位。


Log in to reply