Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/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:

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

    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:

    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.



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


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



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



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



  • 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