StupidBeauty
Read times:2723Posted at:Sat Nov 12 20:08:09 2011

Qt has caught an exception thrown from an event handler. Throwing exceptions from an event handler is not supported in Qt. You must reimplement QApplication::notify() and catch all exceptions there. terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc

在qt写的程序中如果有异常抛出而没有代码捕捉时,最终会被Qt的事件循环捕捉。要想知道到底是哪里抛出的异常,可使用gdb调试,让gdb对“抛出异常”这一情形设置断点,这样,一旦有异常抛出,gdb就会中断程序的运行咯。作为一个图形界面主义者,我不喜欢用命令行进行操作,而QtCreator就让这个过程变得简单咯。在“调试”模式的“断点”视图里单击鼠标右键,“添加断点”,再在“断点类型”里选择“中断 当C++ Exception被抛出”,再确定,就添加好断点咯。再开始调试,出现问题之后就会中断在抛出异常的地方咯。

今天就用这个方式找出咯代码中的一处错误,某個未初始化的整数引起一个整数数组变得巨大,向其中再加入整数元素时因为无法再分配连续的内存块而放弃。

还是没有在代码中加入异常处理的习惯啊。

参考: http://blog.cberger.net/2010/02/24/how-to-find-where-an-exception-is-emited-with-qt/

Your opinions
Your name:Email:Website url:Opinion content:
- no title specified

HxLauncher: Launch Android applications by voice commands