From kde-commits Mon Dec 06 09:57:40 2010 From: =?utf-8?q?Aur=C3=A9lien=20G=C3=A2teau?= Date: Mon, 06 Dec 2010 09:57:40 +0000 To: kde-commits Subject: KDE/kdelibs/kdecore Message-Id: <20101206095740.4D74CAC8A4 () svn ! kde ! org> X-MARC-Message: https://marc.info/?l=kde-commits&m=129162949928338 SVN commit 1204111 by gateau: Do not crash if an job was deleted in a slot connected to result() Note that an assert will trigger if the job is in autoDelete() mode. M +12 -1 jobs/kjob.cpp M +18 -0 tests/kjobtest.cpp M +2 -0 tests/kjobtest.h --- trunk/KDE/kdelibs/kdecore/jobs/kjob.cpp #1204110:1204111 @@ -24,11 +24,13 @@ #include "kjobuidelegate.h" +#include #include #include #include #include #include +#include bool KJobPrivate::_k_kjobUnitEnumRegistered = false; KJobPrivate::KJobPrivate() @@ -302,6 +304,9 @@ Q_D(KJob); d->isFinished = true; + bool autoDelete = isAutoDelete(); + QWeakPointer guard(this); + if ( d->eventLoop ) { d->eventLoop->quit(); } @@ -311,9 +316,15 @@ emit result( this ); - if ( isAutoDelete() ) + if ( autoDelete ) { + Q_ASSERT( guard.data() ); + if ( guard.data() ) { deleteLater(); + } else { + kWarning() << "Job was marked as autoDelete() but has already been deleted!"; } + } +} void KJob::emitPercent( qulonglong processedAmount, qulonglong totalAmount ) { --- trunk/KDE/kdelibs/kdecore/tests/kjobtest.cpp #1204110:1204111 @@ -303,6 +303,24 @@ m_outerJob->exec(); } +void KJobTest::testDeletedInResult() +{ + TestJob *job = new TestJob; + job->setAutoDelete( false ); + + connect( job, SIGNAL( result( KJob* ) ), + this, SLOT( deleteJob( KJob* ) ) ); + + job->start(); + loop.exec(); +} + +void KJobTest::deleteJob(KJob *job) +{ + delete job; + loop.quit(); +} + void KJobTest::slotStartInnerJob() { QTimer::singleShot( 100, this, SLOT( slotFinishOuterJob() ) ); --- trunk/KDE/kdelibs/kdecore/tests/kjobtest.h #1204110:1204111 @@ -79,9 +79,11 @@ void testKill(); void testDelegateUsage(); void testNestedExec(); + void testDeletedInResult(); void slotResult( KJob *job ); void slotFinished(KJob *job); + void deleteJob(KJob *); private: QEventLoop loop;