Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Qt program crash when I delete qwtplot array.
Forum Updated to NodeBB v4.3 + New Features

Qt program crash when I delete qwtplot array.

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 443 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • LeonSuL Offline
    LeonSuL Offline
    LeonSu
    wrote on last edited by
    #1

    OS: Ubuntu 18.02
    GCC version 7.4.0
    QMake Version 3.1
    Qt Version 5.9.5
    QWT Version 6.1.4

    Hello Everyone

    I created a qwtplot array, and set a point on origin at them.

    When I delete the qwtplot array before close windows, the Qt is crashed.

    The Code is show in the following

    mainwindows.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;	//  crash on this line
        delete [] polygon;
        delete [] curve;
        delete [] grid;
        delete [] symbol;
        delete ui;
    }
    

    if I do not to delete the qwtpt array, the Qt debug show the crash on function ~MainWindow()

    if I exchange between qwtpt and curve, the crash is happened on “delete [] curve”

         delete [] curve;	//  crash on this line
        delete [] polygon;
        delete [] qwtpt;
        delete [] grid;
        delete [] symbol;
        delete ui;
    

    Is anyone have idea, or some suggestion for this issues?

    Thanks in advance.

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      You are deleting objects that are properly parented and/or owned. Therefore you are triggering a double delete hence your crash.

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      3
      • LeonSuL Offline
        LeonSuL Offline
        LeonSu
        wrote on last edited by
        #3

        I change method to create the qwtplot object

        from

        QwtPlot *qwtpt = new QwtPlot[numpoints];
        

        to

        QVector<QwtPlot *> qwtplot;
        qwtplot.resize(numpoints );
        for(int k = 0; k < numpoints ; ++k)
            qwtplot = new QwtPlot (this);
        

        So I don't need to delete qwtplot array;
        The Program is worked fine;

        But
        The QwtPlotCurve object is crashed when I set setSymbol before delete it;
        Maybe this is a bug.

        1 Reply Last reply
        0
        • Christian EhrlicherC Offline
          Christian EhrlicherC Offline
          Christian Ehrlicher
          Lifetime Qt Champion
          wrote on last edited by Christian Ehrlicher
          #4

          @LeonSu said in Qt program crash when I delete qwtplot array.:

          So I don't need to delete qwtplot array;

          Using a static c array or a QVector does not change anything if the object must be deleted manually or not.

          The QwtPlotCurve object is crashed when I set setSymbol before delete it;

          A bug on your side I would guess - please show us your current code.

          Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
          Visit the Qt Academy at https://academy.qt.io/catalog

          1 Reply Last reply
          0
          • LeonSuL Offline
            LeonSuL Offline
            LeonSu
            wrote on last edited by
            #5

            Thank you for your reply. Christian Ehrlicher

            The test code as following

            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>
            #include <QVector>
            
            namespace Ui {
            class MainWindow;
            }
            
            class MainWindow : public QMainWindow
            {
                Q_OBJECT
            
            public:
                explicit MainWindow(QWidget *parent = 0);
                ~MainWindow();
            
            private:
                Ui::MainWindow *ui;
            
                const int numpoints = 10;
            
                QVector<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);
            
                polygon = new QPolygonF[numpoints];
                curve = new QwtPlotCurve[numpoints];
                grid = new QwtPlotGrid[numpoints];
                symbol = new QwtSymbol[numpoints];
            
                QString style = "color: white; ";
            
                int k;
                for(k = 0; k < numpoints; ++k)
                    polygon[k] << QPointF(0.0f, 0.0f);
            
                qwtpt.resize(numpoints);
            
                for(k = 0; k < numpoints; ++k){
                    //plot
                    qwtpt[k] = new QwtPlot(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].attach(qwtpt[k]);
                    curve[k].setSamples(polygon[k]);
                    curve[k].setSymbol(&symbol[k]);
            
                    //grid
                    grid[k].attach(qwtpt[k]);
                }
            }
            
            MainWindow::~MainWindow()
            {
            
                delete [] symbol;
                delete [] polygon;
                delete [] grid;
                delete [] curve; //crash on this line
                delete ui;
            }
            

            if I marked "curve[k].setSymbol(&symbol[k]); " in MainWindow, the program will work fine.

            //curve[k].setSymbol(&symbol[k]);
            
            1 Reply Last reply
            0
            • Christian EhrlicherC Offline
              Christian EhrlicherC Offline
              Christian Ehrlicher
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Even if I can't find it directly in the documentation (you may ask the qwt maintainer for this), QwtPlot takes ownership of all QwtPlotItems which are attached via attach(). Therefore you're working on a dangling pointer.

              Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
              Visit the Qt Academy at https://academy.qt.io/catalog

              1 Reply Last reply
              1

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved