Calling QCoreApplication::arguments causes undefined behavior
-
I have a Qt program with this code:
int main(int argc, char **argv){ QApplication app(argc, argv); //Some code app.arguments(); //I know that this isn't supposed to do anything, it's just for debugging purposes return app.exec(); }
Sometimes this code runs just fine, sometimes it crases at the
app.arguments()
line. Why doesQCoreApplication::arguments
crash like that and what can I do about it?I'm using Qt 5.10.1 on 64-bit Windows.
-
I have a Qt program with this code:
int main(int argc, char **argv){ QApplication app(argc, argv); //Some code app.arguments(); //I know that this isn't supposed to do anything, it's just for debugging purposes return app.exec(); }
Sometimes this code runs just fine, sometimes it crases at the
app.arguments()
line. Why doesQCoreApplication::arguments
crash like that and what can I do about it?I'm using Qt 5.10.1 on 64-bit Windows.
@Donald-Duck It shouldn't crash.
Are you sure
//Some code
is not the reason for the crash? -
@Donald-Duck said in Calling QCoreApplication::arguments causes undefined behavior:
app.arguments
Hi,
app.arguments(); doesn't supposed to cause any crash. Maybe your other code before that line cause the problem. -
@Donald-Duck It shouldn't crash.
Are you sure
//Some code
is not the reason for the crash?@aha_1980 @TobbY I'm sure that
arguments()
that crashes. That's at least what the debugger says:As you can see,
arguments()
is the first function in there that isn't part of my code. Apparentlyarguments()
calls other Qt code which causes the program to crash.What's also strange is that when it doesn't crash,
app.arguments().size()
is 0. -
@Donald-Duck said in Calling QCoreApplication::arguments causes undefined behavior:
app.arguments();
Hi,
So, if you comment out app.arguments(); application run without any crash ? right -
I've found the part that causes the problem, but I don't understand why it causes a problem. It's apparently because
app
is of a type that inherits fromQApplication
and not of typeQApplication
directly and because I connected to a signal somewhere. Here is a minimal example:#include <QDebug> #include <QApplication> class MyApplication: public QApplication{ public: MyApplication(int argc, char **argv): QApplication(argc, argv){} }; int main(int argc, char **argv){ MyApplication app(argc, argv); //If I change MyApplication to QApplication, it works as expected QObject::connect(&app, &QApplication::applicationDisplayNameChanged, [](){}); //It does the same thing with any other signal, but without this line it works as expected qDebug() << app.arguments().size(); //This should print 1 or more, but it either prints 0 or crashes return 0; }
The expected behavior would be to print
1
(or more than 1 if I run it with arguments), but it either prints0
or crashes (it seems completely random whether it prints0
or crashes, but it never prints1
as expected). If I either makeapp
of typeQApplication
instead ofMyApplication
or if I comment out theQObject::connect
line, it behaves as expected. -
Hi,
simply add & (pass as reference) in your constructor and it will work perfectly. that is the only culprit in your code.
class MyApplication: public QApplication{ public: MyApplication(int &argc, char** argv): QApplication(argc, argv) { qDebug() <<"Hi "<<argc; } };