diff --git a/src/corelib/thread/qsemaphore.cpp b/src/corelib/thread/qsemaphore.cpp index b11a7051b64..72781942acd 100644 --- a/src/corelib/thread/qsemaphore.cpp +++ b/src/corelib/thread/qsemaphore.cpp @@ -401,10 +401,10 @@ void QSemaphore::release(int n) return; } - { - const auto locker = qt_scoped_lock(d->mutex); - d->avail += n; - } + // Keep mutex locked until after notify_all() lest another thread acquire()s + // the semaphore once d->avail == 0 and then destroys it, leaving `d` dangling. + const auto locker = qt_scoped_lock(d->mutex); + d->avail += n; d->cond.notify_all(); }