5

I am using Qt 4.7 and PyQt 4.7 to build a multi-threaded GUI program. I've been carefully managing PyQt objects so that they stay in one UI thread to avoid synchronization issues and there is no problem in general.

But sometimes, at the moment the python garbage collector is activated from other thread, the destructor of Qt object is called right there and the following assertion fails from inside Qt.

I can define QT_NO_DEBUG even for the debug version and it should be fine because objects being collected hardly cause a synchronization problem. But still, I don't think that's a good idea to turn off other debug messages. How do I prevent this from happening?

#if !defined (QT_NO_DEBUG) || defined (QT_MAC_FRAMEWORK_BUILD)
void QCoreApplicationPrivate::checkReceiverThread(QObject *receiver)
{
    QThread *currentThread = QThread::currentThread();
    QThread *thr = receiver->thread();
    Q_ASSERT_X(currentThread == thr || !thr,
               "QCoreApplication::sendEvent",
               QString::fromLatin1("Cannot send events to objects owned by a different thread. "
                                   "Current thread %1. Receiver '%2' (of type '%3') was created in thread %4")
               .arg(QString::number((quintptr) currentThread, 16))
               .arg(receiver->objectName())
               .arg(QLatin1String(receiver->metaObject()->className()))
               .arg(QString::number((quintptr) thr, 16))
               .toLocal8Bit().data());
    Q_UNUSED(currentThread);
    Q_UNUSED(thr);
}
#elif defined(Q_OS_SYMBIAN) && defined (QT_NO_DEBUG) 

2 Answers 2

6

This problem popped up on the PyQt mailing list in the thread

"subtle bug in PyQt in combination with Python garbage collector"
http://www.riverbankcomputing.com/pipermail/pyqt/2011-August/030376.html

Kovid Goyal proposed a workaround in
http://www.riverbankcomputing.com/pipermail/pyqt/2011-August/030378.html

where he wrote:

As a workaround in my projects, I disable the automatic garbage collector and run garbage collection manually in the GUI thread via QTimer.

He posted a code example there.

Sign up to request clarification or add additional context in comments.

Comments

1

I would recommend using pyqtSignal. You can create signals for your threads to send when their task is completed and the receiver becomes the signal's connected function.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.