Qt6 C++ program crashes when a Dialog window closes
-
wrote on 5 Mar 2024, 19:25 last edited by
I have a Main window and a Dialog window. The Main window has a button that when clicked it opens a modal Dialog window. Then when I close the Dialog window the Main window should still be visible, but the program seems to crash and terminate. If I comment-out the line "my_chart.addSeries(&lineSeries);" in Dialog.cpp, then it doesn't crash. Here are the files:
I added 'charts' to the first line of the .pro file as follows:
QT += core gui chartsmain.cpp
#include "mainwindow.h"
#include <QApplication>int main(int argc, char *argv[])
{
QApplication a(argc, argv);
MainWindow w;
w.show();
return a.exec();
}mainwindow.cpp
#include "mainwindow.h"
#include "ui_mainwindow.h"
#include "dialog.h"MainWindow::MainWindow(QWidget *parent)
: QMainWindow(parent)
, ui(new Ui::MainWindow)
{
ui->setupUi(this);
}MainWindow::~MainWindow()
{
delete ui;
}void MainWindow::on_pushButton_clicked()
{
Dialog myDialog;
myDialog.setModal(true);
myDialog.exec();
}Dialog.h
#ifndef DIALOG_H
#define DIALOG_H#include <QChart>
#include <QDialog>
#include <QLineSeries>namespace Ui {
class Dialog;
}class Dialog : public QDialog
{
Q_OBJECTpublic:
explicit Dialog(QWidget *parent = nullptr);
~Dialog();private slots:
void on_pushButton_clicked();private:
Ui::Dialog *ui;
QLineSeries lineSeries;
QChart my_chart;
int x;
int y;
};
#endif // DIALOG_HDialog.cpp
#include "dialog.h"
#include "ui_dialog.h"Dialog::Dialog(QWidget *parent)
: QDialog(parent)
, ui(new Ui::Dialog)
{
ui->setupUi(this);x = 0; y = 0; lineSeries.append(x, y); my_chart.addSeries(&lineSeries); /* existence of this line causes a crash when exiting the Dialog window */
}
Dialog::~Dialog()
{
delete ui;
}void Dialog::on_pushButton_clicked()
{
x++;
y++;
lineSeries.append(x, y);
}I'm a longtime C programmer, but fairly new to Qt and C++. I'm using Qt 6.6.1 and Qt Creator 12.0.2. The problem seems to be related to the destruction of the Dialog window. I reduced the problem to a fairly small amount of code. If I comment-out the line "my_chart.addSeries(&lineSeries);" in Dialog.cpp, then it doesn't crash. Any help would be appreciated.
-
Hi,
When calling addSeries, as the doc states the QChart takes ownership of the series object. Since you are passing the address of a stack based object, you will have a double deletion happening.
-
Hi,
When calling addSeries, as the doc states the QChart takes ownership of the series object. Since you are passing the address of a stack based object, you will have a double deletion happening.
wrote on 5 Mar 2024, 20:35 last edited by@SGaist Thanks for the quick response. I thought that the call to addSeries(&lineSeries) is passing the address of a NON-stack object since lineSeries is declared/defined in the Dialog class. Are all private variables/objects (defined in 'class Dialog' ) of an instantiated Dialog class stored on the stack? My lack of C++ knowledge is interfering with my C background. And what is a suggested fix to the issue?
-
@SGaist Thanks for the quick response. I thought that the call to addSeries(&lineSeries) is passing the address of a NON-stack object since lineSeries is declared/defined in the Dialog class. Are all private variables/objects (defined in 'class Dialog' ) of an instantiated Dialog class stored on the stack? My lack of C++ knowledge is interfering with my C background. And what is a suggested fix to the issue?
@EricR anything that is not a explicitly allocated on the heap is on the stack.
Make
lineSeries
a pointer and allocated it usingnew
in the constructor. -
1/4