2014-02-11 66 views
5
QApplication.quit

时注意:我还张贴这在PyQt的邮件列表 - 在这里我要回答我的问题,如果一个很好的答案出现在那里。PyQt的:偶尔使用的段错误

执行 QApplication.quit(),可能与libQt5Network.so和/或 QtWebKit的时候我有偶尔段错误的问题。

首先,我使用了3个测试系统:

  • Arch Linux的,PyQt的5.2,Qt的5.2.0,3.3.3的Python
  • 的Ubuntu 13.10,PyQt的5.0.1,QT 5.0.2,3.3.2的Python在VM
  • Windows 7中,PyQt的5.2,Qt的5.2.0,3.3.3的Python

这些事故没有发生拱对我来说,到目前为止,经常在 Ubuntu,并不时在Windows中。 (虽然视窗只是一种猜测 ,我刚刚得到这个python.exe是不工作了 FOO)

初始崩溃

我第一次在一个大的(GER)项目,qutebrowser注意到了这个问题, 它(在Ubuntu)输入:quit时,给了我这个堆栈跟踪:

#0 0xb5c296fc in QMutex::lock()() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#1 0xb3bdd97d in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#2 0xb3bdf0d0 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#3 0xb3bd4418 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#4 0xb3bd8b1e in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#5 0xb5dedf10 in QMetaObject::activate(QObject*, int, int, void**)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#6 0xb5dee48b in QMetaObject::activate(QObject*, QMetaObject const*, int, void**)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#7 0xb5e59155 in QIODevice::readyRead()() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#8 0xb3bb1f14 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#9 0xb3ba4d99 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#10 0xb3bc03bb in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#11 0xb6483a54 in QApplicationPrivate::notify_helper(QObject*, QEvent*)() 
    from /usr/lib/i386-linux-gnu/libQt5Widgets.so.5 
#12 0xb6488e66 in QApplication::notify(QObject*, QEvent*)() 
    from /usr/lib/i386-linux-gnu/libQt5Widgets.so.5 
#13 0xb6bb7e80 in sipQApplication::notify(QObject*, QEvent*)() 
    from /usr/lib/python3/dist-packages/PyQt5/QtWidgets.cpython-33m-i386-linux-gnu.so 
#14 0xb5dc737a in QCoreApplication::notifyInternal(QObject*, QEvent*)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#15 0xb5e11f67 in ??() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#16 0xb5aaf83e in g_main_context_dispatch() 
    from /lib/i386-linux-gnu/libglib-2.0.so.0 
#17 0xb5aafbe8 in ??() from /lib/i386-linux-gnu/libglib-2.0.so.0 
#18 0xb5aafca8 in g_main_context_iteration() 
    from /lib/i386-linux-gnu/libglib-2.0.so.0 
#19 0xb5e1138f in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#20 0xb5dc5c06 in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#21 0xb5dc6014 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#22 0xb5c2b90b in QThread::exec()() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#23 0xb5c2b99b in QThread::run()() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#24 0xb5c2fa08 in ??() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#25 0xb7774d78 in start_thread (arg=0xa5314b40) at pthread_create.c:311 
#26 0xb76ac01e in clone() at ../sysdeps/unix/sysv/linux/i386/clone.S:131 

Core dump here(15MB,gzip的)。

最小示例

然后我与之后的第二与QTimer自动退出本身 最小示例再次尝试。我不得不在一个循环 运行约一分钟左右的时间就发生了:

from PyQt5.QtCore import QUrl, QTimer 
from PyQt5.QtWidgets import QApplication 
from PyQt5.QtWebKitWidgets import QWebView 

app = QApplication([]) 
wv = QWebView() 
wv.load(QUrl("http://www.heise.de/")) 
t = QTimer() 
t.timeout.connect(QApplication.quit) 
t.start(1000) 
wv.show() 
app.exec_() 

这给了我一个非常类似于堆栈跟踪(在Ubuntu):

#0 0xb6cfd8d2 in QCoreApplication::postEvent(QObject*, QEvent*, int)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#1 0xb6d21c83 in QMetaObject::activate(QObject*, int, int, void**)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#2 0xb6d2248b in QMetaObject::activate(QObject*, QMetaObject const*, int, void**)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#3 0xb3e47935 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#4 0xb3dcf687 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#5 0xb3e483b3 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#6 0xb6d21f10 in QMetaObject::activate(QObject*, int, int, void**)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#7 0xb6d2248b in QMetaObject::activate(QObject*, QMetaObject const*, int, void**)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#8 0xb3e43fe5 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#9 0xb3d93b1e in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#10 0xb3d94630 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#11 0xb3d9471b in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#12 0xb6d21f10 in QMetaObject::activate(QObject*, int, int, void**)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#13 0xb6d2248b in QMetaObject::activate(QObject*, QMetaObject const*, int, void**)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#14 0xb6d8d155 in QIODevice::readyRead()() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#15 0xb3e09f14 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#16 0xb3dfcd99 in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#17 0xb3e183bb in ??() from /usr/lib/i386-linux-gnu/libQt5Network.so.5 
#18 0xb492ba54 in QApplicationPrivate::notify_helper(QObject*, QEvent*)() 
    from /usr/lib/i386-linux-gnu/libQt5Widgets.so.5 
#19 0xb4930e66 in QApplication::notify(QObject*, QEvent*)() 
    from /usr/lib/i386-linux-gnu/libQt5Widgets.so.5 
#20 0xb505fe80 in sipQApplication::notify(QObject*, QEvent*)() 
    from /usr/lib/python3/dist-packages/PyQt5/QtWidgets.cpython-33m-i386-linux-gnu.so 
#21 0xb6cfb37a in QCoreApplication::notifyInternal(QObject*, QEvent*)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#22 0xb6d45f67 in ??() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#23 0xb65f483e in g_main_context_dispatch() 
    from /lib/i386-linux-gnu/libglib-2.0.so.0 
#24 0xb65f4be8 in ??() from /lib/i386-linux-gnu/libglib-2.0.so.0 
#25 0xb65f4ca8 in g_main_context_iteration() 
    from /lib/i386-linux-gnu/libglib-2.0.so.0 
#26 0xb6d4536d in QEventDispatcherGlib::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#27 0xb6cf9c06 in QEventLoop::processEvents(QFlags<QEventLoop::ProcessEventsFlag>)() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#28 0xb6cfa014 in QEventLoop::exec(QFlags<QEventLoop::ProcessEventsFlag>)() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#29 0xb6b5f90b in QThread::exec()() 
    from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#30 0xb6b5f99b in QThread::run()() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#31 0xb6b63a08 in ??() from /usr/lib/i386-linux-gnu/libQt5Core.so.5 
#32 0xb7798d78 in start_thread (arg=0xa7812b40) at pthread_create.c:311 
#33 0xb76d001e in clone() at ../sysdeps/unix/sysv/linux/i386/clone.S:131 

Coredump here(15MB,gzip的)。

任何人有一个想法,什么错呢?一些魔术与东西 垃圾收集错误的方式?我也在PyQt4上尝试了一些 解决方法*来解决类似的问题,但是 也没有帮助。

*找不到它被描述的StackOverflow回答 - 基本上 之前运行exec_()设置QtWidgets.qAppQApplication实例,并None之后。

回答

7

我一直在努力追赶段错误的PyQt的/PySide相当长的一段时间。基本上我发现,大多数segfaults可以归咎于图书馆的异步性质(因此也适用于我们)。

就您的确切示例而言,您已将timeout信号连接到quit方法。这里可能发生的情况是,如果超时,quit被调用,并且进程终止时,突然对应用程序对象的所有引用都是无效的。但是当这个操作发生时,QT的事件循环仍在运行,它试图访问它的QNetworkAccessManager来发送另一个信号,但是对该内存位置的引用已经无效,因此发生了段错误。

在这些情况下需要完成的一项工作是执行一种关闭方法,它将确保所有操作都已停止,并以正确的顺序删除正在使用的组件,然后,就在这时,请允许退出。

我写了详细的主题在这里,包括QtWebKit应用安全关机方法:https://github.com/integricho/path-of-a-pyqter/tree/master/qttut08

+0

谢谢!您的教程似乎对其他将来的问题也很有用。如果你有时间,你能否看看我的[执行它](http://git.the-compiler.org/qutebrowser/commit/?id=0abb5cf7384c1ccfb2b829cbf26fcc92add08bc6)(原始崩溃,而不是最小的例子) )看起来合理? –

+1

不客气:)我看了一下你的代码,但是我没有真正测试过。正如我在教程中提到的那样,我注意到的是,这个'deleteLater'方法也是异步的,所以可能发生的情况是您的'shutdown'方法完成,'deleteLater'被安排,并且应用程序退出,但这些对象并没有真正被删除得这么快。我通过将'destroy'信号连接到一个方法来解决这个问题,我检查是否所有组件都被成功删除,然后才允许应用程序退出 – andrean

+0

你说得对,我没有想到这一点。我做了[实现](http://git.the-compiler.org/qutebrowser/commit/?id=8c37e1c33a11fdcb26ac985b72fae27e083b158a),至少在我的(少数)测试中,目前为止没有段错误。 –