Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Must construct a QApplication before a QWidget & Invalid parameter passed to C runtime function

Tags:

c++

debugging

qt

I finished migrating an application from Qt4 to Qt5, it compiles and everything but it crashes at a certain point. I am debugging it and trying to find why but I have reached a dead end:

Here is the stack:

enter image description here

main.cpp line 373:

TouchSwibz w(NULL, NULL, renderMode ? renderMode : AppSettings::RASTERMODE);

When it reaches the breakpoint and I try to go further, it crashes with the usual

"This application has requested the Runtime to terminate it in an unusual way."

And the aplication output shows

QWidget: Must construct a QApplication before a QWidget
Invalid parameter passed to C runtime function.
Invalid parameter passed to C runtime function.

I have thought maybe its because the widget is being initialized when the main window is being created, but what can be done to solve this? What would be a good workaround? I dont even know if this is the real issue. I work under Windows 7 x64 using Qt 5.2.1 and compiling with mingw 4.8 32bit, the application is in 32bits also. Everything is compiled with the same kit. I dont know what other useful information I can provide. I tried stepping inside the QwtSlider constructor but I cant.

like image 228
Victor Avatar asked May 22 '14 10:05

Victor


3 Answers

You're most likely having non-local instances of QWidget type. By definition, those will be initialized before main starts executing, so before QApplication gets constructed. The code below reproduces the problem:

#include <QLabel>
#include <QApplication>

QLabel label("Hello, world!");

int main(int argc, char ** argv)
{
  QApplication app(argc, argv);
  label.show();
  return app.exec();
}

The fix is to delay the construction until there is a QApplication object:

#include <QLabel>
#include <QApplication>

// Won't ever be a dangling pointer.
QPointer<QLabel> label;

int main(int argc, char ** argv)
{
  QApplication app(argc, argv);
  QLabel label_("Hello, world!");
  label.reset(&label_);
  label->show();
  return app.exec();
}
like image 170
Kuba hasn't forgotten Monica Avatar answered Oct 11 '22 15:10

Kuba hasn't forgotten Monica


I managed to solve it by compiling all the libraries in debug mode, turns out having libraries in release mode while building your application in debug mode will make undefined behaviour happen.

like image 27
Victor Avatar answered Oct 11 '22 13:10

Victor


I just solved a similar problem, and here is my detailed situation and solution:

  • I use VS with Qt add-on;

    • and Qt version is 5.7 64 bit (but this is not important)
  • I compiled successfully in both debug and release mode;

  • I could run it in debug mode but not in release, where caused an massage "Must construct a QApplication before a QWidget".

  • [IMPORTANT] I first compiled and tested it under DEBUG mode, and then I met some computational thresh-hold that encouraged me to use RELEASE mode.

  • [IMPORTANT] I used some third-party library, related to Qt GUI component, that requires you to add an external dependency to the project.

  • [IMPORTANT] I just copy the configures in the Project Property page from DEBUG to RELEASE, like exteral C++ library or External Dependencies.

I finally found the reason. In the project's Property Pages dialog box -> Linker folder -> Input property page -> Additional Dependencies, one of the external library should be replaced as a release version one, which have different name, in my case, QGLViewerd2.lib to QGLViewer2.lib.

like image 43
Wuyue Lu Avatar answered Oct 11 '22 15:10

Wuyue Lu