Fix memory leak on new QThreadPool::tryStart version
Also documents the ownership of the traditional tryStart better, and remove a redundant check. Change-Id: I06202465b782926724fa33a901d08c1626f87373 Reviewed-by: Sona Kurazyan <sona.kurazyan@qt.io>
This commit is contained in:
parent
1b4d01d163
commit
b430826501
@ -534,7 +534,7 @@ void QThreadPool::start(std::function<void()> functionToRun, int priority)
|
|||||||
does nothing and returns \c false. Otherwise, \a runnable is run immediately
|
does nothing and returns \c false. Otherwise, \a runnable is run immediately
|
||||||
using one available thread and this function returns \c true.
|
using one available thread and this function returns \c true.
|
||||||
|
|
||||||
Note that the thread pool takes ownership of the \a runnable if
|
Note that on success the thread pool takes ownership of the \a runnable if
|
||||||
\l{QRunnable::autoDelete()}{runnable->autoDelete()} returns \c true,
|
\l{QRunnable::autoDelete()}{runnable->autoDelete()} returns \c true,
|
||||||
and the \a runnable will be deleted automatically by the thread
|
and the \a runnable will be deleted automatically by the thread
|
||||||
pool after the \l{QRunnable::run()}{runnable->run()} returns. If
|
pool after the \l{QRunnable::run()}{runnable->run()} returns. If
|
||||||
@ -549,12 +549,7 @@ bool QThreadPool::tryStart(QRunnable *runnable)
|
|||||||
return false;
|
return false;
|
||||||
|
|
||||||
Q_D(QThreadPool);
|
Q_D(QThreadPool);
|
||||||
|
|
||||||
QMutexLocker locker(&d->mutex);
|
QMutexLocker locker(&d->mutex);
|
||||||
|
|
||||||
if (d->allThreads.isEmpty() == false && d->activeThreadCount() >= d->maxThreadCount)
|
|
||||||
return false;
|
|
||||||
|
|
||||||
return d->tryStart(runnable);
|
return d->tryStart(runnable);
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -571,7 +566,17 @@ bool QThreadPool::tryStart(std::function<void()> functionToRun)
|
|||||||
{
|
{
|
||||||
if (!functionToRun)
|
if (!functionToRun)
|
||||||
return false;
|
return false;
|
||||||
return tryStart(QRunnable::create(std::move(functionToRun)));
|
|
||||||
|
Q_D(QThreadPool);
|
||||||
|
QMutexLocker locker(&d->mutex);
|
||||||
|
if (!d->allThreads.isEmpty() && d->activeThreadCount() >= d->maxThreadCount)
|
||||||
|
return false;
|
||||||
|
|
||||||
|
QRunnable *runnable = QRunnable::create(std::move(functionToRun));
|
||||||
|
if (d->tryStart(runnable))
|
||||||
|
return true;
|
||||||
|
delete runnable;
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*! \property QThreadPool::expiryTimeout
|
/*! \property QThreadPool::expiryTimeout
|
||||||
|
Loading…
Reference in New Issue
Block a user