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
Code:
#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;
}
{
Q_OBJECT
public:
explicit MainWindow
(QWidget *parent
= 0);
~MainWindow();
private:
Ui::MainWindow *ui;
};
mainwindow.cpp
Code:
MainWindow
::MainWindow(QWidget *parent
) : ui(new Ui::MainWindow)
{
ui->setupUi(this);
int numpoints = 10;
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
].
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 position between qwtpt and curve, the crash is happened on “delete [] curve”
Code:
delete [] curve; // crash on this line
delete [] polygon;
delete [] qwtpt;
delete [] grid;
delete [] symbol;
delete ui;
Maybe I must to do something before delete the qwtplot array.
Is anyone have idea, or some suggestion for this issues?
Thanks in advance.
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