summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorIvan Solovev <ivan.solovev@qt.io>2021-12-03 18:13:39 +0100
committerIvan Solovev <ivan.solovev@qt.io>2021-12-15 11:38:39 +0100
commit1806578f2ef67173153f25a4a20dac501d4c9c6e (patch)
tree82357de91f160974bb708346e2ebbb917d8d9489
parentd68134dc320f3f6fc25973aea9e813e4088e4545 (diff)
Windows QBluetoothSocket: fix crash at disconnecting
The last onReadyRead callback can come after the object is destroyed. Protect against it by wrapping 'this' in a QPointer, and checking the QPointer in the callback. Fixes: QTBUG-98719 Change-Id: I3d5200e29744012815cd168a340bc617f85c6540 Reviewed-by: Alex Blasche <alexander.blasche@qt.io> Reviewed-by: Oliver Wolff <oliver.wolff@qt.io> (cherry picked from commit 79a1523ed6262ccd148173ec8732865c0721063a) Reviewed-by: Juha Vuolle <juha.vuolle@insta.fi>
-rw-r--r--src/bluetooth/qbluetoothsocket_winrt.cpp19
1 files changed, 17 insertions, 2 deletions
diff --git a/src/bluetooth/qbluetoothsocket_winrt.cpp b/src/bluetooth/qbluetoothsocket_winrt.cpp
index 731d5432..a1f7212c 100644
--- a/src/bluetooth/qbluetoothsocket_winrt.cpp
+++ b/src/bluetooth/qbluetoothsocket_winrt.cpp
@@ -50,6 +50,7 @@
#include <QtBluetooth/qbluetoothdeviceinfo.h>
#include <QtBluetooth/qbluetoothserviceinfo.h>
#include <QtCore/qloggingcategory.h>
+#include <QtCore/QPointer>
#include <robuffer.h>
#include <windows.devices.bluetooth.h>
@@ -183,7 +184,14 @@ public:
Q_ASSERT_SUCCEEDED(hr);
hr = stream->ReadAsync(buffer.Get(), READ_BUFFER_SIZE, InputStreamOptions_Partial, m_readOp.GetAddressOf());
Q_ASSERT_SUCCEEDED(hr);
- hr = m_readOp->put_Completed(Callback<SocketReadCompletedHandler>(this, &SocketWorker::onReadyRead).Get());
+ QPointer<SocketWorker> thisPtr(this);
+ hr = m_readOp->put_Completed(
+ Callback<SocketReadCompletedHandler>([thisPtr](IAsyncBufferOperation *asyncInfo,
+ AsyncStatus status) {
+ if (thisPtr)
+ return thisPtr->onReadyRead(asyncInfo, status);
+ return S_OK;
+ }).Get());
Q_ASSERT_SUCCEEDED(hr);
return S_OK;
});
@@ -284,7 +292,14 @@ public:
emit socketErrorOccured(QBluetoothSocket::UnknownSocketError);
return S_OK;
}
- hr = m_readOp->put_Completed(Callback<SocketReadCompletedHandler>(this, &SocketWorker::onReadyRead).Get());
+ QPointer<SocketWorker> thisPtr(this);
+ hr = m_readOp->put_Completed(
+ Callback<SocketReadCompletedHandler>([thisPtr](IAsyncBufferOperation *asyncInfo,
+ AsyncStatus status) {
+ if (thisPtr)
+ return thisPtr->onReadyRead(asyncInfo, status);
+ return S_OK;
+ }).Get());
if (FAILED(hr)) {
qErrnoWarning(hr, "onReadyRead(): Failed to set socket read callback.");
emit socketErrorOccured(QBluetoothSocket::UnknownSocketError);