diff options
-rw-r--r-- | .qmake.conf | 2 | ||||
-rw-r--r-- | src/serialport/qwinoverlappedionotifier.cpp | 11 |
2 files changed, 11 insertions, 2 deletions
diff --git a/.qmake.conf b/.qmake.conf index 0b51738b..33171164 100644 --- a/.qmake.conf +++ b/.qmake.conf @@ -2,4 +2,4 @@ load(qt_build_config) DEFINES += QT_NO_FOREACH QT_NO_JAVA_STYLE_ITERATORS QT_NO_LINKED_LIST -MODULE_VERSION = 5.15.12 +MODULE_VERSION = 5.15.13 diff --git a/src/serialport/qwinoverlappedionotifier.cpp b/src/serialport/qwinoverlappedionotifier.cpp index 2090004b..8ec5c1a8 100644 --- a/src/serialport/qwinoverlappedionotifier.cpp +++ b/src/serialport/qwinoverlappedionotifier.cpp @@ -396,8 +396,8 @@ void QWinOverlappedIoNotifierPrivate::notify(DWORD numberOfBytes, DWORD errorCod Q_Q(QWinOverlappedIoNotifier); WaitForSingleObject(hResultsMutex, INFINITE); results.enqueue(IOResult(numberOfBytes, errorCode, overlapped)); - ReleaseMutex(hResultsMutex); ReleaseSemaphore(hSemaphore, 1, NULL); + ReleaseMutex(hResultsMutex); // Do not send a signal if we didn't process the previous one. // This is done to prevent soft memory leaks when working in a completely // synchronous way. @@ -417,6 +417,15 @@ void QWinOverlappedIoNotifierPrivate::_q_notified() WaitForSingleObject(hResultsMutex, INFINITE); QQueue<IOResult> values; results.swap(values); + // Decreasing the semaphore count to keep it in sync with the number + // of messages in queue. As ReleaseSemaphore does not accept negative + // values, this is sort of a recommended way to go: + // https://learn.microsoft.com/en-us/windows/win32/api/synchapi/nf-synchapi-releasesemaphore#remarks + int numToDecrease = values.size() - 1; + while (numToDecrease > 0) { + WaitForSingleObject(hSemaphore, 0); + --numToDecrease; + } ReleaseMutex(hResultsMutex); // 'q' can go out of scope if the user decides to close the serial port // while processing some answer. So we need to guard against that. |