runAsync: Fix issue with deleting thread

deleteLater schedules the event in the current thread, so we
may not use it directly on a thread from within the thread itself...
Instead connect the threads finished signal to its deleteLater slot.
To avoid problems if runAsync is not run from the main thread,
move the thread to the main thread before starting it.

Task-number: QTCREATORBUG-15688
Change-Id: Iea003d00c58d1f921fb0b4ddf9cf67dcb1379833
Reviewed-by: Tobias Hunger <tobias.hunger@theqtcompany.com>
This commit is contained in:
Eike Ziller
2016-01-29 10:42:55 +01:00
committed by Tobias Hunger
parent 1edd6193ba
commit 9dc6f8b47d
2 changed files with 4 additions and 2 deletions

View File

@@ -39,7 +39,6 @@ void RunnableThread::run()
m_runnable->run();
if (m_runnable->autoDelete())
delete m_runnable;
deleteLater();
}
} // Internal

View File

@@ -682,7 +682,10 @@ QFuture<ResultType> runAsync(Function &&function, Args&&... args)
auto job = new Internal::AsyncJob<ResultType,Function,Args...>
(std::forward<Function>(function), std::forward<Args>(args)...);
QFuture<ResultType> future = job->future();
(new Internal::RunnableThread(job))->start(); // automatically deletes itself
auto thread = new Internal::RunnableThread(job);
thread->moveToThread(qApp->thread()); // make sure thread gets deleteLater on main thread
QObject::connect(thread, &QThread::finished, thread, &QObject::deleteLater);
thread->start();
return future;
}