【发布时间】: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