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. Layout crashes (when the program ends, but after all code in the program has executed???)
QtWS25 Last Chance

Layout crashes (when the program ends, but after all code in the program has executed???)

Scheduled Pinned Locked Moved General and Desktop
7 Posts 3 Posters 1.2k Views
  • 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.
  • M Offline
    M Offline
    Michael.R.LegakoL-3com.com
    wrote on last edited by
    #1

    All,
    I have a small Qt program that crashes apparently after the last of my code executes??? I've pared the code down to only 3 small files:

    main.cpp

    @
    #include <QApplication>
    #include "mainwindow.h"

    int main(int argc, char *argv[])
    {
    QApplication a(argc, argv);
    MainWindow w;
    w.show();
    return a.exec();
    }
    @

    mainwindow.h

    @
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QWidget>
    #include <QLabel>
    #include <QVBoxLayout>
    class MainWindow : public QMainWindow
    {
    Q_OBJECT
    public:
    MainWindow(QWidget *parent = 0);
    ~MainWindow();
    QLabel m_oSpectrogram_ch1;
    QWidget m_CentralWidget;
    QVBoxLayout *m_pMainLayout;
    };
    #endif // MAINWINDOW_H
    @

    mainwindow.cpp

    @
    #include <QMainWindow>
    #include <QVBoxLayout>
    #include "mainwindow.h"
    MainWindow::MainWindow(QWidget *parent)
    : QMainWindow(parent)
    {

    m_pMainLayout = new QVBoxLayout;
    m_pMainLayout->addWidget(&m_oSpectrogram_ch1);
    
    m_CentralWidget.setLayout(m_pMainLayout);
    setCentralWidget(&m_CentralWidget);
    

    }

    MainWindow::~MainWindow()
    {
    delete m_pMainLayout;
    return;
    }
    @

    Any suggestions?

    [edit: added missing coding tags @ SGaist]

    1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Don't delete m_pMainLayout, it's set on m_CentralWidget and will be deleted when it's destroyed.

      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
      • M Offline
        M Offline
        Michael.R.LegakoL-3com.com
        wrote on last edited by
        #3

        SGaist,
        Dude you REALLY are a mad scientist! I only posted a couple of minutes ago...

        I did try not deleting the m_pMainLayout. The program still crashes on exit. So apparently the program can even be one line smaller and still crash... I can break in the destructor of MainWindow, and see that it did execute, and in main I can break on the return statement. The program seems to crash when return a.exec() executes.

        1 Reply Last reply
        0
        • SGaistS Offline
          SGaistS Offline
          SGaist
          Lifetime Qt Champion
          wrote on last edited by
          #4

          You should also allocate m_CentralWidget and m_oSpectrogram_ch1 on the heap rather than the stack

          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
          • M Offline
            M Offline
            Michael.R.LegakoL-3com.com
            wrote on last edited by
            #5

            SGaist has it RIGHT!

            #include <QMainWindow>
            #include <QFormLayout>
            #include "mainwindow.h"
            MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            {

            m_pMainLayout = new QVBoxLayout;
            m_pMainLayout->addWidget(&m_oSpectrogram_ch1);
            
            m_pCentralWidget = new QWidget;
            m_pCentralWidget->setLayout(m_pMainLayout);
            setCentralWidget(m_pCentralWidget);
            

            }

            MainWindow::~MainWindow()
            {

            }

            The above code does NOT crash on exit...

            Thanks!

            1 Reply Last reply
            0
            • A Offline
              A Offline
              alex_malyu
              wrote on last edited by
              #6

              [[blank-post-content-placeholder]]

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                You should also allocate m_oSpectrogram_ch1 on the heap

                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

                • Login

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