From 9dc6f8b47deabdd6e0787ab62694e9fbdd85ef3e Mon Sep 17 00:00:00 2001 From: Eike Ziller Date: Fri, 29 Jan 2016 10:42:55 +0100 Subject: [PATCH] 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 --- src/libs/utils/runextensions.cpp | 1 - src/libs/utils/runextensions.h | 5 ++++- 2 files changed, 4 insertions(+), 2 deletions(-) diff --git a/src/libs/utils/runextensions.cpp b/src/libs/utils/runextensions.cpp index 4fff2612a60..69cb3882e85 100644 --- a/src/libs/utils/runextensions.cpp +++ b/src/libs/utils/runextensions.cpp @@ -39,7 +39,6 @@ void RunnableThread::run() m_runnable->run(); if (m_runnable->autoDelete()) delete m_runnable; - deleteLater(); } } // Internal diff --git a/src/libs/utils/runextensions.h b/src/libs/utils/runextensions.h index 775d61af30f..9984a4b72cb 100644 --- a/src/libs/utils/runextensions.h +++ b/src/libs/utils/runextensions.h @@ -682,7 +682,10 @@ QFuture runAsync(Function &&function, Args&&... args) auto job = new Internal::AsyncJob (std::forward(function), std::forward(args)...); QFuture 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; }