Results 1 to 2 of 2

Thread: Qt program crash when I delete qwtplot array.

  1. #1
    Join Date
    Nov 2019
    Posts
    2
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows

    Question Qt program crash when I delete qwtplot array.

    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.

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

    The Code is show in the following

    mainwindows.h
    Qt Code:
    1. #include <QMainWindow>
    2. #include <qwt_plot.h>
    3. #include <qwt_plot_curve.h>
    4. #include <qwt_plot_grid.h>
    5. #include <qwt_plot_canvas.h>
    6. #include <qwt_plot_curve.h>
    7. #include <qwt_plot_grid.h>
    8. #include <qwt_symbol.h>
    9. #include <QPolygonF>
    10. namespace Ui {
    11. class MainWindow;
    12. }
    13. class MainWindow : public QMainWindow
    14. {
    15. Q_OBJECT
    16. public:
    17. explicit MainWindow(QWidget *parent = 0);
    18. ~MainWindow();
    19. private:
    20. Ui::MainWindow *ui;
    21. QwtPlot *qwtpt;
    22. QPolygonF *polygon;
    23. QwtPlotCurve *curve;
    24. QwtPlotGrid *grid;
    25. QwtSymbol *symbol;
    26. };
    To copy to clipboard, switch view to plain text mode 

    mainwindow.cpp
    Qt Code:
    1. MainWindow::MainWindow(QWidget *parent) :
    2. QMainWindow(parent),
    3. ui(new Ui::MainWindow)
    4. {
    5. ui->setupUi(this);
    6. int numpoints = 10;
    7. qwtpt = new QwtPlot[numpoints];
    8. polygon = new QPolygonF[numpoints];
    9. curve = new QwtPlotCurve[numpoints];
    10. grid = new QwtPlotGrid[numpoints];
    11. symbol = new QwtSymbol[numpoints];
    12. QString style = "color: white; ";
    13. QPolygonF initialpoint;
    14. initialpoint << QPointF(0.0f, 0.0f);
    15. int k;
    16. for(k = 0; k < numpoints; ++k){
    17. //plot
    18. qwtpt[k].setParent(ui->scrollArea);
    19. qwtpt[k].setGeometry(10, 20 + 181 * k, 760, 161);
    20. qwtpt[k].setAxisTitle(QwtPlot::xBottom, "points");
    21. qwtpt[k].setAxisTitle(QwtPlot::yLeft, "efficience %");
    22. qwtpt[k].setAxisScale(QwtPlot::xBottom, 0.0, 132);
    23. qwtpt[k].setAxisScale(QwtPlot::yLeft, 0.0, 100);
    24. qwtpt[k].setStyleSheet(style);
    25. //symbol
    26. symbol[k].setPen(QPen(Qt::red, 2));
    27. symbol[k].setStyle(QwtSymbol::Ellipse);
    28. symbol[k].setBrush(QBrush(Qt::yellow));
    29. symbol[k].setSize(QSize(8,8));
    30. //curve
    31. curve[k].setPen(Qt::blue, 4);
    32. curve[k].setRenderHint(QwtPlotItem::RenderAntialiased, true);
    33. curve[k].setSymbol(&symbol[k]);
    34. curve[k].attach(&qwtpt[k]);
    35. curve[k].setSamples(initialpoint);
    36. //grid
    37. grid[k].attach(&qwtpt[k]);
    38. }
    39. }
    40. MainWindow::~MainWindow()
    41. {
    42. delete [] qwtpt; // crash on this line
    43. delete [] polygon;
    44. delete [] curve;
    45. delete [] grid;
    46. delete [] symbol;
    47. delete ui;
    48. }
    To copy to clipboard, switch view to plain text mode 

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

    if I exchange position between qwtpt and curve, the crash is happened on “delete [] curve”
    Qt Code:
    1. delete [] curve; // crash on this line
    2. delete [] polygon;
    3. delete [] qwtpt;
    4. delete [] grid;
    5. delete [] symbol;
    6. delete ui;
    To copy to clipboard, switch view to plain text mode 

    Maybe I must to do something before delete the qwtplot array.

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

    Thanks in advance.

  2. #2
    Join Date
    Feb 2006
    Location
    Munich, Germany
    Posts
    3,309
    Thanked 879 Times in 827 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Qt program crash when I delete qwtplot array.

    All QObjects ( widgets are objects ) are owned by their parent, that automatically deletes them. The same is with plot item ( f.e curves ) and QwtPlot.
    So better allocate the plots one by one with new and don't delete manually, when not necessary.

    Uwe

Similar Threads

  1. Replies: 0
    Last Post: 21st November 2019, 07:26
  2. Crash application after delete window
    By ecspertiza in forum Qt Quick
    Replies: 5
    Last Post: 20th October 2016, 18:36
  3. Application crashes when I delete the character pointer array
    By arunkumaraymuo1 in forum Qt Programming
    Replies: 4
    Last Post: 12th February 2013, 21:26
  4. QwtPlot crash in Mac OS X
    By sirmojtaba in forum Qwt
    Replies: 2
    Last Post: 21st August 2009, 03:28
  5. crash when delete QTreeWidgetItem*
    By evgenM in forum Qt Programming
    Replies: 9
    Last Post: 9th December 2006, 21:04

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.