From 809f007d111b41263ff80ca8dcd157048cb88d03 Mon Sep 17 00:00:00 2001 From: Juha Vuolle Date: Wed, 25 Jan 2023 13:29:15 +0200 Subject: Catch broadcast receiver unregistration exception on Android Reproducing the issue reliably has proven to be near impossible, but the stack trace can be seen every now and then, and it does not seem to cause any functional harm. The strong suspicion on what happens is that in some places of the code we create a temporary QBluetoothLocalDevice object to check for example address(). This happens for example when creating a QLowEnergyController object, which is one of the scenarios I recall seeing the unhandled exception. The localdevice creation triggers broadcast receiver registration, and the ~immediate destruction triggers unregistration. Reading the address() works as it does not rely on any broadcast events. It seems plausible that in some cases the registration is not yet fully complete on Android side when the unregistration is ~immediately called. Fixes: QTBUG-106938 Change-Id: Iac734f17fb9d2a1879fca39d9b2839cb530e5df5 Reviewed-by: Ivan Solovev (cherry picked from commit 29e73453ed653782fd885e79974de76bd60fc734) Reviewed-by: Qt Cherry-pick Bot --- .../qt/android/bluetooth/QtBluetoothBroadcastReceiver.java | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothBroadcastReceiver.java b/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothBroadcastReceiver.java index 7b163efb..6c74c3e3 100644 --- a/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothBroadcastReceiver.java +++ b/src/android/bluetooth/src/org/qtproject/qt/android/bluetooth/QtBluetoothBroadcastReceiver.java @@ -51,7 +51,11 @@ public class QtBluetoothBroadcastReceiver extends BroadcastReceiver { synchronized (qtContext) { qtObject = 0; - qtContext.unregisterReceiver(this); + try { + qtContext.unregisterReceiver(this); + } catch (Exception ex) { + Log.d(TAG, "Trying to unregister a BroadcastReceiver which is not yet registered"); + } } } -- cgit v1.2.3