<div dir="ltr">Hi all,<br><br>We have some problems with sending python objects between threads by signals with Qt.QueuedConnection type. If python object was sent from one thread to another by signal, its reference counter does not increment, and object can be destroyed in first thread before slot in second thread will be called which leads to a crash.<br><br>I made example which illustrates this situation (also available on github: <a href="https://gist.github.com/nxsofsys/d80487e7605eb7061d88">https://gist.github.com/nxsofsys/d80487e7605eb7061d88</a> ):<br><br>from PyQt5.QtCore import QObject, QThread, pyqtSignal<br>from PyQt5.QtCore import qDebug, QCoreApplication, Qt<br>import time<br>import sys<br><br>class SomeThread(QObject):<br><br> signal = pyqtSignal(QObject)<br><br> def __init__(self):<br> QObject.__init__(self)<br> self.thread = QThread()<br> self.wait = self.thread.wait<br> #<br> # self.signal.connect(self.signalHandler, type = Qt.QueuedConnection)<br> self.moveToThread(self.thread)<br> self.signal.connect(self.signalHandler, type = Qt.QueuedConnection)<br> self.thread.started.connect(self.run)<br> self.thread.start()<br><br> def signalHandler(self, obj):<br> qDebug("signalHandler obj ref counts: " + str(sys.getrefcount(obj)))<br><br> def run(self):<br> qDebug("Thread sleeps for 1 sec")<br> time.sleep(1)<br> qDebug("Process events now")<br> QCoreApplication.processEvents()<br> self.thread.exit()<br><br>app = QCoreApplication([])<br>th = SomeThread()<br><br># obj = QObject()<br>def someFunc():<br> obj = QObject()<br> qDebug("obj ref counts before emit: " + str(sys.getrefcount(obj)))<br> th.signal.emit(obj)<br> qDebug("obj ref counts after emit: " + str(sys.getrefcount(obj)))<br><br>someFunc()<br>qDebug("Process main events")<br>QCoreApplication.processEvents()<br>qDebug("Process main events done")<br>th.thread.wait()<br><br>I think in this case signal emit should increment reference counter for a python object preventing it from destruction. Just comment "obj = QObject" in someFunc and uncomment "# obj = QObject()" before function definition, and script runs without crash.<br><br>Thanks,<br>-- <br><div class="gmail_signature">Ilya Volkov<br><a href="mailto:nxsofsys@gmail.com" target="_blank">nxsofsys@gmail.com</a></div>
</div>