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. Strange Crash When Widgets show() is called
QtWS25 Last Chance

Strange Crash When Widgets show() is called

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 3 Posters 2.5k 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.
  • B Offline
    B Offline
    BriFuture
    wrote on last edited by
    #1

    A Strange Crash as long as widget (or inherent from QWidget) show() method is called. I'm using custom Application which subclass QApplication, would that be the problem?

    Platform: Ubuntu 18.04,  Qt5.9
    

    Debug Information:

    6cb0bdd5-6a3a-4f99-b037-a48ab0e84ceb-image.png

    I'll try it later on windows platform (with Qt5.7 installed).

    1 Reply Last reply
    0
    • B Offline
      B Offline
      BriFuture
      wrote on last edited by
      #2

      Now I tested the same code on Windows10 platform, with Both Qt5.7 and Qt5.12, the program seems fine and no error occured. After all, the widgets shows as normal. But on linux, it crashed immediately when show() is called.

      Now it seems that different platform may perform different actions.
      I will write a simple demo project to illustrate the problem if error case can be reproduced.

      1 Reply Last reply
      0
      • B Offline
        B Offline
        BriFuture
        wrote on last edited by
        #3

        Yeah, now I reproduced the case with simple project created by QtCreator Qt Widgets Application Template. Three files in the project:

        main:

        // file: main.cpp
        #include "MainWindow.h"
        
        #define MyCustomApp CustomApp
        //#define MyCustomApp QApplication
        
        int main(int argc, char *argv[])
        {
            MyCustomApp a(argc, argv);
            MainWindow w;
            w.show();
            return a.exec();
        }
        
        

        MainWindow:

        // file: MainWindow.h
        
        #ifndef MAINWINDOW_H
        #define MAINWINDOW_H
        
        #include <QMainWindow>
        #include <QApplication>
        
        class CustomApp : public QApplication
        {
            Q_OBJECT
        public:
            CustomApp(int argc, char *argv[]) : QApplication(argc, argv)
            {
            }
        };
        
        QT_BEGIN_NAMESPACE
        namespace Ui { class MainWindow; }
        QT_END_NAMESPACE
        
        class MainWindow : public QMainWindow
        {
            Q_OBJECT
        
        public:
            MainWindow(QWidget *parent = nullptr);
            ~MainWindow();
        
        private:
            Ui::MainWindow *ui;
        };
        #endif // MAINWINDOW_H
        
        
        // file: MainWindow.cpp
        #include "MainWindow.h"
        #include "ui_MainWindow.h"
        
        MainWindow::MainWindow(QWidget *parent)
            : QMainWindow(parent)
            , ui(new Ui::MainWindow)
        {
            ui->setupUi(this);
        }
        
        MainWindow::~MainWindow()
        {
            delete ui;
        }
        

        Now, run this project with CustomApp under DEBUG mode:
        9c0fe4d8-79de-47eb-be2e-6123cb56882e-image.png

        it crashed.

        JonBJ 1 Reply Last reply
        0
        • B BriFuture

          Yeah, now I reproduced the case with simple project created by QtCreator Qt Widgets Application Template. Three files in the project:

          main:

          // file: main.cpp
          #include "MainWindow.h"
          
          #define MyCustomApp CustomApp
          //#define MyCustomApp QApplication
          
          int main(int argc, char *argv[])
          {
              MyCustomApp a(argc, argv);
              MainWindow w;
              w.show();
              return a.exec();
          }
          
          

          MainWindow:

          // file: MainWindow.h
          
          #ifndef MAINWINDOW_H
          #define MAINWINDOW_H
          
          #include <QMainWindow>
          #include <QApplication>
          
          class CustomApp : public QApplication
          {
              Q_OBJECT
          public:
              CustomApp(int argc, char *argv[]) : QApplication(argc, argv)
              {
              }
          };
          
          QT_BEGIN_NAMESPACE
          namespace Ui { class MainWindow; }
          QT_END_NAMESPACE
          
          class MainWindow : public QMainWindow
          {
              Q_OBJECT
          
          public:
              MainWindow(QWidget *parent = nullptr);
              ~MainWindow();
          
          private:
              Ui::MainWindow *ui;
          };
          #endif // MAINWINDOW_H
          
          
          // file: MainWindow.cpp
          #include "MainWindow.h"
          #include "ui_MainWindow.h"
          
          MainWindow::MainWindow(QWidget *parent)
              : QMainWindow(parent)
              , ui(new Ui::MainWindow)
          {
              ui->setupUi(this);
          }
          
          MainWindow::~MainWindow()
          {
              delete ui;
          }
          

          Now, run this project with CustomApp under DEBUG mode:
          9c0fe4d8-79de-47eb-be2e-6123cb56882e-image.png

          it crashed.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @BriFuture
          From the stack trace, QCoreApplication::arguments() is implicated. I'd look at your argc/argv passing, and your strings in argv[] being right.

          • Try debugging them out in your CustomApp constructor code?
          • Try debugging out QCoreApplication::arguments() before you get to w.show()?
          • Try calling MyCustomApp a(1, { "MyCustomApp", 0 }); instead of the actual command line?
          • The signature the Qt docs give is actually QApplication::QApplication(int &argc, char **argv). Note the int &. It's probably irrelevant, but try declaring your CustomApp constructor parameters like that, just in case it matters?
          1 Reply Last reply
          0
          • B Offline
            B Offline
            BriFuture
            wrote on last edited by
            #5

            @JonB said in Strange Crash When Widgets show() is called:

            implicated

            24a610ee-245a-476c-93fa-03fa1bc895f1-image.png

            The problem is figured out, I change the signature of the CustomApp's constructor: CustomApp(int &argc, char *argv[]).

            It can run as normal now.

            JonBJ 1 Reply Last reply
            1
            • B BriFuture

              @JonB said in Strange Crash When Widgets show() is called:

              implicated

              24a610ee-245a-476c-93fa-03fa1bc895f1-image.png

              The problem is figured out, I change the signature of the CustomApp's constructor: CustomApp(int &argc, char *argv[]).

              It can run as normal now.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by
              #6

              @BriFuture

              The problem is figured out, I change the signature of the CustomApp's constructor: CustomApp(int &argc, char *argv[]).

              I did wonder about that, hence suggestion you alter your signature to match the Qt one. And I think it's the int & instead of int that is at issue. But if some C++ expert wishes to explain how it worked and what went wrong with the original way you wrote it, I'm all ears :)

              B 2 Replies Last reply
              0
              • JonBJ JonB

                @BriFuture

                The problem is figured out, I change the signature of the CustomApp's constructor: CustomApp(int &argc, char *argv[]).

                I did wonder about that, hence suggestion you alter your signature to match the Qt one. And I think it's the int & instead of int that is at issue. But if some C++ expert wishes to explain how it worked and what went wrong with the original way you wrote it, I'm all ears :)

                B Offline
                B Offline
                BriFuture
                wrote on last edited by
                #7
                This post is deleted!
                1 Reply Last reply
                0
                • JonBJ JonB

                  @BriFuture

                  The problem is figured out, I change the signature of the CustomApp's constructor: CustomApp(int &argc, char *argv[]).

                  I did wonder about that, hence suggestion you alter your signature to match the Qt one. And I think it's the int & instead of int that is at issue. But if some C++ expert wishes to explain how it worked and what went wrong with the original way you wrote it, I'm all ears :)

                  B Offline
                  B Offline
                  BriFuture
                  wrote on last edited by
                  #8

                  @JonB

                  Year. Actually the problem is bypassed while not solved. Your advice is helpful and the code runs so I thought the post can be solved.

                  Actually I am not sure why the same code runs fine on Windows but not Ubuntu. I've tried several means and one of them can make the code runnable on Ubuntu:

                  1. disable QML debugging and profiling
                  2. change qmake build config to Release instead of Debug. So the qmake build step is : qmake: qmake customapp.pro -spec linux-g++
                  3. Rebuild the project.
                  4. Run. MainWindow Shows, no errors reported.

                  But I dont think this solution is elegant.
                  By the way, I did not remember whether it's Release build or Debug build on My Windows Platform. The problem may be caused by Debug configure ?
                  I'd recheck my project configuration on windows platform.

                  1 Reply Last reply
                  0
                  • B Offline
                    B Offline
                    BriFuture
                    wrote on last edited by
                    #9

                    Ubuntu Platform

                    Now I try Release version of the program with CustomApp's constructor parameter as (int , char *[]) and the mainwindow shows normally. But it still crashes under Debug mode.

                    Here is the modified main.c :

                    #include "MainWindow.h"
                    #include <QPointer>
                    #include <QDebug>
                    #include <iostream>
                    
                    int main(int argc, char *argv[])
                    {
                    #ifdef QT_DEBUG
                        std::cout << argc << (" Debug---- ") << argv[0] << std::endl;
                    #else
                        std::cout << argc << (" Release---- ") << argv[0] << std::endl;
                    #endif
                        CustomApp app(argc, argv);
                        qDebug() << QCoreApplication::arguments();
                        MainWindow w;
                        w.show();
                        qDebug() << "MainWindow shows";
                        return app.exec();
                    }
                    

                    under Debug:
                    ac5a1e70-6ca5-4479-bdf4-1f0ab6e0db6e-image.png
                    under Release"
                    4443a424-e2d3-45c5-b12f-6b2480c3f857-image.png

                    So, why does the qcoreapplication need parameter as int & instread of int?

                    Em... I would build debug version of qt later to check what's wrong it is.

                    1 Reply Last reply
                    0
                    • B Offline
                      B Offline
                      BriFuture
                      wrote on last edited by
                      #10

                      Actually, the signature of CustomApp as CustomApp(int argc, char *argv[]) still works under debug build.

                      1 Reply Last reply
                      0
                      • Christian EhrlicherC Online
                        Christian EhrlicherC Online
                        Christian Ehrlicher
                        Lifetime Qt Champion
                        wrote on last edited by
                        #11

                        Simply fix the signature to pass a reference to int and all is working as expected.

                        Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                        Visit the Qt Academy at https://academy.qt.io/catalog

                        1 Reply Last reply
                        2

                        • Login

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