【问题标题】:Exception handling doesn't work with Qt on Windows异常处理不适用于 Windows 上的 Qt
【发布时间】:2011-05-08 03:21:50
【问题描述】:

我遇到了奇怪的问题。也就是说,Qt 以某种方式关闭了我程序中的异常处理。我无法捕获任何异常,并且当我抛出异常时应用程序崩溃。

我在 Windows 7(64 位)上使用来自 Qt SDK v2010.05 的 Qt 4.7.0(32 位),来自 MinGW 的 g++ (GCC) 4.5.1,NetBeans 6.9.1。 但我也用 g++ 3.4.5(也来自 MinGW)和 Qt Creator 2.0.1 对此进行了检查——同样的奇怪行为。

例如(最简单的情况):

#include <Qt/QApplication.h>
#include <iostream>
#include <stdexcept>
#include <cstdlib>

using namespace std;


int main(int argc, char* argv[]) {
    QApplication app(argc, argv);

    try {
        cout << "Before exception" << endl;
        throw runtime_error("Exception occured");
        cout << "After exception" << endl;
    } catch (runtime_error& exc) {
        cout << exc.what() << endl;
        exit(1);
    }

    return 0;
}

当我执行上面的程序时,我得到了这个输出:

异常前

此应用程序已请求运行时以不寻常的方式终止它。
请联系应用程序的支持团队了解更多信息。

我尝试将标志“-fexceptions”添加到 g++,但它没有改变任何东西。

当我不使用 Qt 时,一切正常:

#include <Qt/QApplication.h> // It is not caused only by including Qt header
                             // so it doesn't matter if I comment this out or not
#include <iostream>
#include <stdexcept>
#include <cstdlib>

using namespace std;


int main(int argc, char* argv[]) {
    // QApplication app(argc, argv);

    try {
        cout << "Before exception" << endl;
        throw runtime_error("Exception occured");
        cout << "After exception" << endl;
    } catch (runtime_error& exc) {
        cout << exc.what() << endl;
        exit(1);
    }

    return 0;
}

输出:

异常前
发生异常

有谁知道为什么会这样以及如何解决这个问题?与构建 Qt 时使用的异常处理方法(SJLJ 或 Dwarf-2)的类型有关吗?

【问题讨论】:

  • 哇,你吓到我了!根据我的建议,我们可能即将切换到 Qt,这将是一个杀手。好在它适用于 VS。
  • 它到底在哪里崩溃了?你有机会找到更准确的位置吗? QApplication 的 notify() 也许?
  • 可能它配置了-no-exceptions 标志。尝试重新配置并重新制作 Qt SDK。
  • 嗯,这是逐字 Microsoft CRT 消息。 mingw 能很好地模拟它 that 吗?令人怀疑,可能是这个问题的原因。
  • @Hans Passant:Mingw 链接到 MSVCRT。

标签: c++ windows exception qt4 mingw


【解决方案1】:

我已经重新配置并重新编译了带有标志-exceptions的Qt:
D:\Qt\2010.05\qt&gt;mingw32-make confclean &amp;&amp; configure -exceptions &amp;&amp; mingw32-make
现在一切正常!

感谢大家的帮助,尤其是 Nick D!

不管怎样,我在构建 Qt 时没有这个标志是很奇怪的。我已经从官方网站下载了二进制形式的 Qt SDK。

【讨论】:

    【解决方案2】:

    不再需要在 Qt 中使用 -exceptions 标志。在 Qt Creator 4 中,这是默认设置,我的 Windows Qt 应用程序愉快地使用了广泛而广泛的异常处理,没有任何问题。 Qt MSVC 构建使用 /EHsc 编译器选项,打开正常的异常处理。

    【讨论】:

      猜你喜欢
      • 2017-07-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-08
      • 1970-01-01
      • 2011-07-29
      • 2013-12-25
      • 2017-08-23
      相关资源
      最近更新 更多