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. Qt6 C++ program crashes when a Dialog window closes
Servers for Qt installer are currently down

Qt6 C++ program crashes when a Dialog window closes

Scheduled Pinned Locked Moved Solved General and Desktop
4 Posts 2 Posters 298 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.
  • E Offline
    E Offline
    EricR
    wrote on 5 Mar 2024, 19:25 last edited by
    #1

    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 charts

    main.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_OBJECT

    public:
    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_H

    Dialog.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.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      SGaist
      Lifetime Qt Champion
      wrote on 5 Mar 2024, 19:34 last edited by
      #2

      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.

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

      E 1 Reply Last reply 5 Mar 2024, 20:35
      3
      • S SGaist
        5 Mar 2024, 19:34

        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.

        E Offline
        E Offline
        EricR
        wrote on 5 Mar 2024, 20:35 last edited by
        #3

        @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?

        S 1 Reply Last reply 5 Mar 2024, 20:39
        0
        • E EricR
          5 Mar 2024, 20:35

          @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?

          S Offline
          S Offline
          SGaist
          Lifetime Qt Champion
          wrote on 5 Mar 2024, 20:39 last edited by
          #4

          @EricR anything that is not a explicitly allocated on the heap is on the stack.

          Make lineSeries a pointer and allocated it using new in the constructor.

          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
          0
          • E EricR has marked this topic as solved on 6 Mar 2024, 12:04

          1/4

          5 Mar 2024, 19:25

          • Login

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