Unknown Exception is not beeing caught in trycatch-Block (Bluetooth Low Energy, ServiceDetailsDiscovery)
-
Hi,
I have developed a GUI Application for connecting to Bluetooth Low Energy (BLE) devices and discovering their services and service details. My BLE device actually has 5 services, the Generic Access Service and the Generic Attribute Service, amoung others. Most of the time when I run the application, everything works fine - my 5 services are discovered and then I can discover the service details as the follwing:
void BluetoothModel::startServiceDetailsDiscovery(QBluetoothUuid serviceUuid) { selectedService= lowEnergyController->createServiceObject(serviceUuid, this); connect(selectedService, &QLowEnergyService::stateChanged, this, &TestApp::serviceDetailsDiscoveredSlot); selectedService->discoverDetails(); }
But sometimes, the App is only discovering 4 Services instead of 5 - the Generic Access Service is missing. When I then try to discover the service details of one of the other services, the App crashes without any exception. So therefore I used a tryCatch Block beacuse I do not know what kind of exception this is:
void BluetoothModel::startServiceDetailsDiscovery(QBluetoothUuid serviceUuid) { selectedService= lowEnergyController->createServiceObject(serviceUuid, this); connect(selectedService, &QLowEnergyService::stateChanged, this, &TestApp::serviceDetailsDiscoveredSlot); try{ selectedService->discoverDetails(); }catch(std::exception &e){ qDebug() << "error, try it again!" << e.what(); } }
Then I ran the App multiple times until there were only 4 services again (everytime the same service is missing). When I then tried to discover the service details of one of the other services, the App crashes again without the exception being caught or my debug-String "error..." printed. So how is this possible? I know from debugging, that exactly this line of code
selectedService->discoverDetails();
is responsible for the error. Is there another better way to debug this or an alternative to try catch? I wanted to use Signals and Slot but I do not know which kind of exception occurs.Thank you
-
@JonB Yes, I did.
@KH-219Design : Thank you very much for your suggestion, for using the lowenergy example from Qt. I did this and the same error occurs there! For this reason, I copied the following code from there and paste it in my .cpp file:
connect(controller, QOverload<QLowEnergyController::Error>::of(&QLowEnergyController::error), this, &Device::errorReceived);
And in the Slot, I had:
void BluetoothModel::errorReceived(QLowEnergyController::Error /*error*/) { qWarning() << "Error: " << controller->errorString(); }
And after a couple of times of connecting and disconneting, finally the error occurs. My
errorReceived
said:Error: "Remote device closed the connection"
And in the debugger, I had:
But my "Problem" was, that the LED on my device, that indactes that it is connected (it is a blue LED), was still on, so I did not knew before, that the connection was closed.
So, it has to do with my device and not with my code. Now, knowing where the error occurs, I can reestablish the connection to the device, before the user can do anything on he GUI, that leads to the crash.@All: Thank you very much for your help here, love this community :)
-
@SpaceToon
It depends what you mean by "crashes". Things like access violations or segmentation faults do not throw C++ exceptions which can be caught. You are better running the application under the host debugger and seeing what the stack trace/information is when it crashes, which the debugger should catch. You could then try showing those details here, and see if an expert can spot what the issue might be.Also, just as a BTW, it may not be
selectedService->discoverDetails();
per se which causes the crash. Since you have connected a slot, assuming that slot is called in the course of/afterdiscoverDetails()
it might be in the slot code that there is a problem. Again, a stack trace would tell you. -
@JonB Thank you! By "crashing" I mean that my application closes immediately
When I run the application in debug-mode, this occurs, when only 4 services are discovered and when I click on discover service details:Then I cannot move or click on the GUI. When I then press on "Continue CDB for TestAppl" in the Debugger, the Exception window above occurs again and I still cannot do anything in my App.
I have enabled logging with
(QLoggingCategory::setFilterRules(QStringLiteral("qt.bluetooth* = true")));
My device which I connect to is Adafruit Left Hand:When only 4 services are found, this is the output
qt.bluetooth.winrt: Worker started qt.bluetooth.winrt: BTLE scan completed qt.bluetooth.winrt: BT scan completed qt.bluetooth.winrt: Discovered BT device: "136757781040785" "Echo-8HJ" Num UUIDs 0 qt.bluetooth.winrt: Discovered BT device: "251311382725287" "AirPods" Num UUIDs 1 qt.bluetooth.winrt: Discovered BT device: "9809398457619" "Soundcore Liberty Neo" Num UUIDs 3 qt.bluetooth.winrt: Discovered BT device: "203368100016156" "Philips PH802" Num UUIDs 2 qt.bluetooth.winrt: onBluetoothLEDeviceFound: No device given qt.bluetooth.winrt: Discovered BTLE device: "140679378455177" "Bluetooth 7f:f2:78:61:4a:89" Num UUIDs 0 RSSI: -79 Num manufacturer data 0 qt.bluetooth.winrt: onBluetoothLEDeviceFound: No device given qt.bluetooth.winrt: Discovered BTLE device: "262804901115837" "Adafruit Left Hand" Num UUIDs 1 RSSI: -88 Num manufacturer data 0 qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: onBluetoothLEDeviceFound: No device given qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: Discovered BTLE device: "83630596497393" "Bluetooth 4c:0f:c3:ff:1f:f1" Num UUIDs 0 RSSI: -79 Num manufacturer data 0 qt.bluetooth.winrt: Using new low energy controller qt.bluetooth.winrt: QLowEnergyControllerPrivateWinRTNew::connectToDevice qt.bluetooth.winrt: QLowEnergyControllerPrivateWinRTNew::registerForStatusChanges qt.bluetooth.winrt: Updating data for device "Adafruit Left Hand" "EF:05:08:C6:B7:BD" qt.bluetooth.winrt: Service discovery initiated qt.bluetooth.winrt.service.thread: QLowEnergyControllerPrivateWinRTNew::onServiceDiscoveryFinished Changing service pointer from thread QThread(0x29584305210) /**#######################################################################*/ //These lines only appears when only 4 services are discovered. qt.bluetooth.winrt: Unregistering 0 value change tokens qt.bluetooth.winrt: QLowEnergyControllerPrivateWinRTNew::unregisterFromStatusChanges / /**#######################################################################*/ qt.bluetooth.winrt.service.thread: QLowEnergyControllerPrivateWinRTNew::onServiceDiscoveryFinished Changing service pointer from thread QThread(0x29584305210) qt.bluetooth.winrt.service.thread: QLowEnergyControllerPrivateWinRTNew::onServiceDiscoveryFinished Changing service pointer from thread QThread(0x29584305210) qt.bluetooth.winrt: Discovered BTLE device: "87415138462507" "Bluetooth 4f:80:ec:46:ab:2b" Num UUIDs 0 RSSI: -84 Num manufacturer data 0 qt.bluetooth.winrt.service.thread: QLowEnergyControllerPrivateWinRTNew::onServiceDiscoveryFinished Changing service pointer from thread QThread(0x29584305210) qt.bluetooth.winrt.service.thread: QLowEnergyControllerPrivateWinRTNew::onServiceDiscoveryFinished Changing service pointer from thread QThread(0x29584305210)
So
-
@SpaceToon
This is perhaps useful information for others to look at. But what I wanted was the stack trace when the exception occurred. You are showing the Issues pane. There is another window in the debugger, for Stack Trace or similar. -
Granted that Microsoft Windows is not the environment that I currently work in, I nonetheless believe that:
read access violation at: 0x0
... can only mean one thing, and that thing is: some code is attempting to dereference a null pointer.
(someone please correct me if you know otherwise).
Likewise,
read access violation at 0xffffffff
would mean that something tried to read from the address0xffffffff
@SpaceToon I find it encouraging that you posted screenshots from the debugger. We can see that you are successfully running your application under control of a debugger. Now (as @JonB has also said), you just need to find the area of the debugger that shows the call stack (or "stack trace" or "crash stack").
-
Hi,
@SpaceToon said in Unknown Exception is not beeing caught in trycatch-Block (Bluetooth Low Energy, ServiceDetailsDiscovery):
createServiceObject
You are not checking the return value of that method before using it. There lies your issue.
One such check is shown in the Qt Bluetooth Low Energy overview.
-
@SpaceToon you also mentioned not knowing what kinds of exception are happening.
Note that in one of your screenshots, the debugger is listing many rows that look like:
Debugger encountered an exception: Exception at ----, code: 0xnnnnnnnn
Those codes are meaningful in a Windows setting:
(got these by googling):
0x80010105: RPC_E_SERVERFAULT
0x40080202: WinRT transform error
0x40080201: WinRT originate error -
@JonB and @KH-219Design : Thank you both. Well, when the error occurs in debug mode and when I then go on View -> Views -> Stack, and check it, this View occurs. But do you mean this? I do not see any information which might be helpful..:
And the Debugger-Arrow in the code shows to the line:
try{ selectedService->discoverDetails();
@SGaist : that's a good point thank you. I added the following line to my code:
if(!selectedService){ qDebug() << "Selected Service not found! Please Start the Service seacrh again"; return; }
But the error still occurs. So the service, which I try to discover the details of, does exist...
-
@SpaceToon make sure
selectedService
is initialized with a nullptr and that you set it to nullptr each time you destroy the object pointed to it, or the check won't work. -
@JonB :
@J-Hilk Yes, It is initialized with a nullptr, and when I click on reset on the GUI (or when the function startServiceDetailsDiscovery is called, I actually make the following check, which I did not copy in the code in my above post due to clarity):
if(selectedService) { delete selectedService; selectedService = nullptr; }
-
@SpaceToon
OK, not a lot more helpful in the further trace, other than knowing it happens duringQMdiArea::viewMode()
.But I think the Level #4 line is telling you that you have source code and the error emanated when Line #160 of
BluetoothModel.cpp
was executing. May not help, but you might look at that line of code for clues? -
@JonB said in Unknown Exception is not beeing caught in trycatch-Block (Bluetooth Low Energy, ServiceDetailsDiscovery):
But I think the Level #4 line is telling you that you have source code and the error emanated when Line #160 of BluetoothModel.cpp was executing. May not help, but you might look at that line of code for clues?
Thank you. Well, that's exactly this line:
try { selectedService->discoverDetails(); }
What I don't understand: Okay, that only 4 services are found and displayed instead of 5 is an error. I still don't know why this error occurs sometimes (and I cannot specifically reproduce the error. I keep connecting to the device until the error occurs at some point). Since I don't need the missing service, it doesn't matter at first that it is missing. But the other service I use, the Nordic UART Service, does exist and is being found. Then when I try to get the details from this service, my application crashes. If I could catch the error, then I would simply start the search for the services again automatically, because then it will find all 5 services again and the application runs correctly. But because of the crash, this possibility is missing. But I thank you guys for the help anyway.
-
@SpaceToon
I notice that everything you are using --- including the actual Qt libraries --- is compiled (MSVC) with debug.Have you tried recompiling/linking for Release mode? There is a "reasonable" chance the crash will not happen there... !
P.S.
Have you tried commenting out your:connect(selectedService, &QLowEnergyService::stateChanged, this, &TestApp::serviceDetailsDiscoveredSlot);
Does the crash still happen during
selectedService->discoverDetails()
? If not, did you ever show us yourTestApp::serviceDetailsDiscoveredSlot()
? -
@SpaceToon thank you for posting the screenshot of the crash stack.
If this were my project and I were debugging, my normal behavior is to focus on the last (top-most) part of the stack where I recognize function names that I myself authored. In the present case, that would be
BluetoothModel.cpp
line 160. (This echoes what @JonB said earlier, so I know you have done this already.)However, it is possible that the bug is in Qt code and not in
BluetoothModel.cpp
.It is always advantageous, however, to do everything possible to rule out a bug in one's own code, because it is generally a quite larger effort to both: (1) diagnose a bug in Qt itself, and (2) patch a bug in Qt itself (or wait for a patch).
If I focus on the Qt part of the stack, however, then the crash appears to happen in
QLowEnergyController::connected
(very top, "Level 1" or frame 1 of the stack).What seems weird to me is that
QLowEnergyController::connected
is a signal. And I cannot (given my lack of deeper access to your debugger scenario) superficially see how execution could logically proceed fromBluetoothModel::startServiceDetailsDiscovery
("Level 4" of stack) intoQLowEnergyController::connected
("Level 3" of stack).You might want to look at any code of yours that references that signal (
QLowEnergyController::connected
).If this is a Qt bug (specific to Qt BLE on the WinRT system), then you might try to reproduce it by building and running the "Bluetooth Low Energy Scanner Example":
- https://github.com/qt/qtconnectivity/tree/5.15.1/examples/bluetooth/lowenergyscanner
- https://doc.qt.io/qt-5/qtbluetooth-lowenergyscanner-example.html
If you try connecting to your peripheral from the official Qt sample app and you encounter a crash when doing so, then I think https://bugreports.qt.io would accept that.
-
You're probably overwriting a pointer or double-deleting an object.
selectedService = lowEnergyController->createServiceObject(serviceUuid, this);
This looks quite fishy. What happens in your model if I run through that twice and then run through the removal code (assuming there's one) twice ... I'd've expected you keep the objects in a list or something ...
-
@KH-219Design Thank you, I will try that approach.
@kshegunov : Well, everytime, this function is called, I check if the selectedService Object already exists, nd if so, I delete it. So the follwoing function is called, when the user selects a Service in the GUI and clicks on Dsicover service details:
if(selectedService) { delete selectedService; selectedService = nullptr; } selectedService = lowEnergyController->createServiceObject(serviceUuid, this); if(!selectedService){ qDebug() << "Service not found" return; } connect(selectedService, &QLowEnergyService::stateChanged, this, &BluetoothModel::serviceDetailsDiscoveredSlot); try { selectedService->discoverDetails(); } catch (std::exception &e) { qDebug() << "error, try it again!" << e.what(); }
So as you can see here, When the user clicks on the button more than one time (for no reason), there is always a check that makes sure, that the pointer to the selecteService obejct is not overwritten. Is that what you meant by your question?
-
@SpaceToon
Looks OK to me.In a previous post I asked you:
Have you tried commenting out your:
connect(selectedService, &QLowEnergyService::stateChanged, this, &TestApp::serviceDetailsDiscoveredSlot);
If not, did you ever show us your
TestApp::serviceDetailsDiscoveredSlot()
?I may be barking up the wrong tree, but is there any reason you don't want to try this or show your slot which is called during the crashy code? Perhaps you know that this is 100% irrelevant?
-
@JonB Sorry, I saw your post, but I forgot to answer here. Well, if I comment this line, then nothing happens after the service discovery. I think this is not relevant, but here is the serviceDetailsDiscoveredSlot:
void BluetoothModel::serviceDetailsDiscoveredSlot(QLowEnergyService::ServiceState newState) { if (newState == QLowEnergyService::ServiceDiscovered) { characteristicsList = selectedService->characteristics(); for (const QLowEnergyCharacteristic &characteristic : characteristicsList) { emit characteristicDiscoveredSignal(characteristic); } }
And the characteristicDiscoveredSignal only tells the UI to show the discovered characteristics on the GUI.
-
@SpaceToon said in Unknown Exception is not beeing caught in trycatch-Block (Bluetooth Low Energy, ServiceDetailsDiscovery):
Well, if I comment this line, then nothing happens after the service discovery
I don't understand what that means. Your crash then goes away if this code not executed??
characteristicsList = selectedService->characteristics();
--- couldselectedService == nullptr/inavlid
? "And the characteristicDiscoveredSignal only tells the UI to show the discovered characteristics on the GUI` --- and could that cause a crash?As I said, maybe I'm barking up the wrong tree from the traceback, your call, just trying to see where a "crash" could be....