简体   繁体   English

必须在将QWidget和Invalid参数传递给C运行时函数之前构造QApplication

[英]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. 我完成了从Qt4到Qt5的应用程序的迁移,它编译了所有内容,但它在某个时刻崩溃了。 I am debugging it and trying to find why but I have reached a dead end: 我正在调试它,并试图找到原因,但我已达到死胡同:

Here is the stack: 这是堆栈:

在此输入图像描述

main.cpp line 373: main.cpp第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. 我使用Qt 5.2.1在Windows 7 x64下工作,使用mingw 4.8 32bit编译,应用程序也在32位。 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. 我试着踩到QwtSlider构造函数但我不能。

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

You're most likely having non-local instances of QWidget type. 您最有可能拥有QWidget类型的非本地实例。 By definition, those will be initialized before main starts executing, so before QApplication gets constructed. 根据定义,这些将在main开始执行之前初始化,因此在构建QApplication之前。 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: 修复是延迟构造,直到有一个QApplication对象:

#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 ; 我使用VS和Qt附加组件 ;

    • and Qt version is 5.7 64 bit (but this is not important) 和Qt版本是5.7 64位(但这并不重要)
  • 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". 我可以在调试模式下运行它,但不能在发布版本中运行它,这会导致按钮“必须在QWidget之前构建一个QApplication”。

  • [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. [重要]我首先在DEBUG模式下编译和测试它,然后我遇到了一些鼓励我使用RELEASE模式的计算阈值。

  • [IMPORTANT] I used some third-party library , related to Qt GUI component, that requires you to add an external dependency to the project. [重要]我使用了一些与Qt GUI组件相关的第三方库 ,它要求您向项目添加外部依赖项。

  • [IMPORTANT] I just copy the configures in the Project Property page from DEBUG to RELEASE, like exteral C++ library or External Dependencies. [重要]我只是将Project Property页面中的配置从DEBUG复制到RELEASE,如外部C ++库或外部依赖项。

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 . 项目的“属性页”对话框 - >“链接器”文件夹 - >“输入属性”页 - >“其他依赖项”中 ,应将其中一个外部库替换为发行版本1,它具有不同的名称,在我的情况下, QGLViewerd2.libQGLViewer2.lib

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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