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 BothQt5.7
andQt5.12
, the program seems fine and no error occured. After all, the widgets shows as normal. But on linux, it crashed immediately whenshow()
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:
it crashed.
-
@BriFuture
From the stack trace,QCoreApplication::arguments()
is implicated. I'd look at yourargc
/argv
passing, and your strings inargv[]
being right.- Try debugging them out in your
CustomApp
constructor code? - Try debugging out
QCoreApplication::arguments()
before you get tow.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 theint &
. It's probably irrelevant, but try declaring yourCustomApp
constructor parameters like that, just in case it matters?
- Try debugging them out in your
-
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 ofint
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 :) -
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:
disable
QML debugging and profiling- change qmake build config to
Release
instead ofDebug
. So the qmake build step is :qmake: qmake customapp.pro -spec linux-g++
- Rebuild the project.
- 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 byDebug
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 underDebug
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:
under Release"
So, why does the qcoreapplication need parameter as
int &
instread ofint
?Em... I would build debug version of qt later to check what's wrong it is.
-
Simply fix the signature to pass a reference to int and all is working as expected.