简体   繁体   中英

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

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:

在此输入图像描述

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.

我设法通过在调试模式下编译所有库来解决它,结果发布库处于发布模式,而在调试模式下构建应用程序将导致未定义的行为发生。

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();
}

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM