[Qt-interest] QObject::deleteLater and private event loop (not using QCoreApplication::exec())
Giuseppe D'Angelo
dangelog at gmail.com
Sun Jun 12 09:41:57 CEST 2011
> deleteLater() events (DeferredDelete) are handled specially, to avoid
> something like:
>
> obj->deleteLater();
> obj->something();
> run a dialog
> obj->somethingElse(); // crash here
>
> DeferredDelete events are only handled at the same loop nesting level that
> they were created in or less deep nesting. They are never handled by deeper
> nesting levels.
The only culprit I found when writing
http://developer.qt.nokia.com/wiki/Threads_Events_QObjects is that if
you are at level "0" of nesting event loops, the event will be picked
up and your items get deleted (pretty much reasonable, since Qt
doesn't know about any "outer" loop that will eventually perform the
deletion).
For instance:
int main(int argc, char **argv)
{
QApplication app(argc, argv);
QWeakPointer<QObject> obj(new QObject);
obj.data()->deleteLater();
QEventLoop e;
QTimer::singleShot(0, &e, SLOT(quit()));
e.exec();
// or:
// QDialog d;
// QTimer::singleShot(0, &d, SLOT(accept()));
// d.exec();
qDebug() << obj.data();
delete obj.data();
return 0;
}
Will print QObject(0x0).
--
Giuseppe D'Angelo
More information about the Qt-interest-old
mailing list