UE5 crash with "EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000053"

Hi,

I have written a custom C++ UActorComponent using c++/winrt api to scan and connect to BLE devices. I have successfully tested the core code running in a console app before “porting” over to UE5.4.3; after some trial and error getting the code to compile under UE c++ project, I received the following error when running in editor play mode:

Unhandled Exception: EXCEPTION_ACCESS_VIOLATION reading address 0x0000000000000053

ucrtbase
ucrtbase
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_Core
UnrealEditor_BLE_CSC_Component!<lambda_befdac169625a2c62afa34bbd90fd52c>::operator()<winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher>() [D:\Unreal Projects\BLEPluginDev\Plugins\BLE_CSC_Component\Source\BLE_CSC_Component\Private\BLECSCSensorComponent.cpp:105]
UnrealEditor_BLE_CSC_Component!winrt::impl::delegate<winrt::Windows::Foundation::TypedEventHandler<winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementWatcher,winrt::Windows::Devices::Bluetooth::Advertisement::BluetoothLEAdvertisementR() [C:\Program Files (x86)\Windows Kits\10\Include\10.0.19041.0\cppwinrt\winrt\Windows.Foundation.h:896]
Windows_Devices_Bluetooth
Windows_Devices_Bluetooth
Windows_Devices_Bluetooth
Windows_Devices_Bluetooth
Windows_Devices_Bluetooth
ntdll
ntdll
kernel32
ntdll

The lambda function referenced in the stack trace is as follows:

winrt::event_token scanner_received_token_ = scanner_.Received(
    [&scan_is_active_, &scan_stop_cv_, &scan_stop_mutex_, this](const auto& w, const BluetoothLEAdvertisementReceivedEventArgs args) {

        std::string identifier = winrt::to_string(args.Advertisement().LocalName());

        //Get service UUIDs from advertisement packet.  Note, this is an incomplete list of service UUIDs but should contain the CSC service.
        auto services = args.Advertisement().ServiceUuids();
        std::string macAddress = _mac_address_to_str(args.BluetoothAddress());
        UE_LOG(LogTemp, Display, TEXT("Found device %s, mac: %s, with %d services:\n"), *identifier.c_str(), *macAddress.c_str(), services.Size());

        bool hasCSCService = false;
        
        for (auto service : services) {   //BLECSCSensorComponent.cpp:105
            std::string uuidString = guid_to_string(service);
            UE_LOG(LogTemp, Display, TEXT("\tservice %ls \n"), *uuidString.c_str());
            ...

I am at a loss trying to figure out what is the root cause. This part of the logic has been tested without any problem when I ran it as a console app outside of UE. Furthermore, if there is an error accessing services object, why wouldn’t the exception get raised a few lines above it in the UE_LOG call when it’s referenced via services.Size(). Any help is greatly appreciated!

After numerous trial and errors, I believe the crash was caused by missing #include <winrt/Windows.Foundation.Collections.h>. I am new to both Unreal Engine and C++/WinRT (I know, not an ideal combination) so I am not clear why this turned into a runtime error and not a compile time error…

BTW, the UE_LOG call before the for each loop turned out also causes errors, c_str() needs to be converted into FString first…

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.