Unknown Exception is not beeing caught in trycatch-Block (Bluetooth Low Energy, ServiceDetailsDiscovery)
-
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....
-
@JonB
Ohh sorry, I was using a older version of my code and was commenting out the wrong line ;d
I will try it now with the correct version. I think this will take time ro reproduce this, but I will report here when I'm done.EDIT: That was fast. The applications till crashes, although I commented out this line. And I do not think that my characteristicDiscoveredSignal caused a crash because I set up a breakpoint there and it is never reached.
-
When I set a breakpoint at
selectedService->discoverDetails();
and when I want to jump in the qt functiondiscoverDetails()
while debugging, it does not work. I want to now at which point exactly the error appears. Is there another way to debug this qt-own-function? -
@SpaceToon
From your earlier stack trace, it looks like you are linking against debug versions of the Qt libraries, but there is a lack of line numbers. Did you compile Qt for debug, and can the debugger see/know the location of the corresponding Qt source files? -
@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 :)