[SOLVED] App crashes when closing; some objects not closed? MWE



  • Hi!

    My app compiles and works fine, but crashes when i close it.

    I have read about this issue in the forum. Some people had solved this problem after they have removed all objects that they allocated on the heap which already had a QObject parent, but they had set them additionally to: "delete somewidget;" before.

    I just have:

    MainWindow::~MainWindow()
    {
        delete mainWindow;
    }
    

    to delete the mainWindow.

    So I guess, I am missing some objects which I should close too? Or is it another issue?

    I have produced a minimal working example:
    mainwindow.h

    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    
    #include <QMainWindow>
    
    
    class QTabWidget;
    class QWidget;
    class QLabel;
    class QGroupBox;
    class QLineEdit;
    class QPushButton;
    class QRadioButton;
    class QHBoxLayout;
    class QFormLayout;
    class QDoubleValidator;
    class QPalette;
    
    
    
    class MainWindow : public QMainWindow
    {
        Q_OBJECT
    
    public:
        explicit MainWindow(QWidget *parent = 0);
        ~MainWindow();
    
    private slots:
        void startfile();
        void tabcalc();
    
    private:
        void setCurrentIndex();
    
        QMainWindow *mainWindow;
        QTabWidget *tabWidget;
    };
    
    
    
    
    
    class WelcomeTab : public QWidget
    {
        Q_OBJECT
    public:
        WelcomeTab(QWidget *parent = 0);
    };
    
    
    
    class TabCalc : public QWidget
    {
        Q_OBJECT
    public:
        TabCalc(QWidget *parent = 0);
    
    private slots:
        void calculate();
    
    private:
        QGroupBox *method();
        QGroupBox *input();
        QGroupBox *output();
        QGroupBox *calc();
        QRadioButton *radio1;
        QRadioButton *radio2;
        QLineEdit *inputnumber;
        QLineEdit *output;
    };
    
    #endif // MAINWINDOW_H
    
    

    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 <QtGui>
    #include <QObject>
    #include <cmath>
    #include <QPalette>
    #include <QLabel>
    #include <QHBoxLayout>
    #include <QGroupBox>
    #include <QRadioButton>
    #include <QLineEdit>
    #include <QPushButton>
    #include <QFormLayout>
    #include <QDoubleValidator>
    #include <QPalette>
    
    
    MainWindow::MainWindow(QWidget *parent) :
        QMainWindow(parent)
    {
        tabWidget = new QTabWidget;
        tabWidget->addTab(new WelcomeTab(), tr("Welcome"));
        tabWidget->addTab(new TabCalc(), tr("Calculate"));
        setCentralWidget(tabWidget);
    
        setWindowTitle(tr("Calculate A or B"));
        setMinimumSize(100, 100);
    }
    
    
    WelcomeTab::WelcomeTab(QWidget *parent)
        : QWidget(parent)
    {
        QLabel *welcomeLabel = new QLabel
                    (tr("Welcome"));
    
        QVBoxLayout *mainLayout = new QVBoxLayout;
        mainLayout->addWidget(welcomeLabel);
        setLayout(mainLayout);
    }
    
    void MainWindow::startfile()
    {
        tabWidget->setCurrentIndex(0);
    }
    
    
    TabCalc::TabCalc(QWidget *parent)
        : QWidget(parent)
    {
        QGridLayout *grid = new QGridLayout;
        grid->addWidget(method(), 0, 0);
        grid->addWidget(input(), 0, 1);
        grid->addWidget(output(), 0, 2);
        grid->addWidget(calc(), 0, 3);
        setLayout(grid);
    }
    
    QGroupBox *TabCalc::method()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Method"));
        radio1 = new QRadioButton(tr("Choice A"));
        radio2 = new QRadioButton(tr("Choice B"));
    
        QVBoxLayout *vbox = new QVBoxLayout;
         vbox->addWidget(radio1);
         vbox->addWidget(radio2);
         vbox->addStretch(1);
         groupBox->setLayout(vbox);
         return groupBox;
    }
    
    QGroupBox *TabCalc::input()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Input"));
        inputnumber = new QLineEdit;
    
        QFormLayout *formLayout = new QFormLayout;
          formLayout->addRow(tr("Insert a number [-10;10]"), inputnumber);
          groupBox->setLayout(formLayout);
    
          QValidator *validator = new QDoubleValidator(-10, 10, 10, this);
          inputnumber->setValidator(validator);
          return groupBox;
    }
    
    QGroupBox *TabCalc::output()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Output"));
        QLabel *label = new QLabel(tr("A or B"));
        output = new QLineEdit;
    
        QFormLayout *formLayout = new QFormLayout;
          formLayout->addRow(label);
          formLayout->addRow(tr("Result:"), output);
          groupBox->setLayout(formLayout);
          return groupBox;
    }
    
    QGroupBox *TabCalc::calc()
    {
        QGroupBox *groupBox = new QGroupBox(tr("Calculate"));
        QPushButton *pushButton = new QPushButton(tr("Calculate"));
    
        QVBoxLayout *vbox = new QVBoxLayout;
         vbox->addWidget(pushButton);
         vbox->addStretch(1);
         groupBox->setLayout(vbox);
         connect(pushButton, SIGNAL(clicked()),this, SLOT(calculate()));
         return groupBox;
    }
    
    void TabCalc::calculate()
    {
        double result;
        bool ok;
        double A=10;
        double B=20;
        double insertedNumber = inputnumber->text().toDouble(&ok);
    
    if(radio1->isChecked())
        {
           result = insertedNumber+A;
           QString resultString = "";
           output->setText(resultString.setNum(result));
        }
        else if(radio2->isChecked())
        {
           result = insertedNumber+B;
           QString resultString = "";
           output->setText(resultString.setNum(result));
        }
        else
        {
           result = 0;
           output->setText("Choose A or B");
        }
    
    }
    
    
    void MainWindow::tabcalc()
    {
       tabWidget->setCurrentIndex(1);
    }
    
    MainWindow::~MainWindow()
    {
        delete mainWindow;
    }    
    

    Please let me know, if you see the problem!


  • Lifetime Qt Champion

    Hi,

    AFAICS, you don't use nor initialize that mainWindow variable so technically you are trying to delete something that doesn't exist



  • @SGaist, he man, that's great!

    Now it works!


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.