Important: Please read the Qt Code of Conduct -

Strange Crash When Widgets show() is called

  • 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:


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

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

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


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


    // file: MainWindow.h
    #ifndef MAINWINDOW_H
    #define MAINWINDOW_H
    #include <QMainWindow>
    #include <QApplication>
    class CustomApp : public QApplication
        CustomApp(int argc, char *argv[]) : QApplication(argc, argv)
    namespace Ui { class MainWindow; }
    class MainWindow : public QMainWindow
        MainWindow(QWidget *parent = nullptr);
        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)
        delete ui;

    Now, run this project with CustomApp under DEBUG mode:

    it crashed.

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

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



    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.

  • @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 :)

  • This post is deleted!

  • @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 -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.

  • 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;
        std::cout << argc << (" Release---- ") << argv[0] << std::endl;
        CustomApp app(argc, argv);
        qDebug() << QCoreApplication::arguments();
        MainWindow w;;
        qDebug() << "MainWindow shows";
        return app.exec();

    under Debug:
    under Release"

    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.

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

  • Lifetime Qt Champion

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

Log in to reply