Abort on BLEAddress tostring

dwaynez
Posts: 2
Joined: Thu Dec 15, 2022 6:48 pm

Abort on BLEAddress tostring

Postby dwaynez » Thu Dec 15, 2022 7:00 pm

I am scanning for BLE devices. When I check to see if it is a device that I am looking for, I print a message to the serial log. Every now and then, the ESP32 reboots because of an abort. The backtrace shows the problem is with the tostring function on BLEAddress. Any ideas?

Code: Select all

#include <BLEDevice.h>
#define allowed_devices  "ff:ff:20:00:26:53,ff:ff:80:02:13:f9,e9:99:8d:f2:65:ce,f9:54:a9:22:48:0d"      


class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
            if (strstr(allowed_devices,advertisedDevice.getAddress().toString().c_str()) != NULL) {
                ble_found = true;
                if (!ble_connected) {
                    ble_device = String(advertisedDevice.getName().c_str());
                        char strBuf[100];
                        sprintf(strBuf, "Device authorized %s(%s) %i", advertisedDevice.getName().c_str(),advertisedDevice.getAddress().toString().c_str(),rssi);
                        PRINTLN(strBuf);
                    ble_connected = true;
                }
    } // onResult
}; // MyAdvertisedDeviceCallbacks

// When scanning complete, start all over again looking for a device
void ScanCompleteCB(BLEScanResults scanResults) {
    if (!ble_found and ble_connected) {
        ble_connected = false;
        }
    start_scan = true;
} // scanCompleteCB

void setup() {
    pBLEScan = BLEDevice::getScan();
    pBLEScan->setAdvertisedDeviceCallbacks(new MyAdvertisedDeviceCallbacks());
    pBLEScan->setActiveScan(true);
 }

// Main processing loop
void loop() {
    if (start_scan) {
        ble_found = false;
        pBLEScan->start(5, ScanCompleteCB, false);
        // PRINTLN("Starting BLE scan");
        // wifi_send(t_state,"Starting BLE scan");
        start_scan = false;
Here is my decode of the trace:
0x4008f670: invoke_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 156
0x4008f8ed: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 171
0x401ccbff: __cxxabiv1::__terminate(void (*)()) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 47
0x401ccc46: std::terminate() at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 57
0x401cc56d: __cxxabiv1::__cxa_allocate_exception(std::size_t) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_alloc.cc line 268
0x401cc648: operator new(unsigned int) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/new_op.cc line 54
0x401cdab5: std::__cxx11::basic_string, std::allocator >::_M_create(unsigned int&, unsigned int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/ext/new_allocator.h line 104
0x401cdd03: std::__cxx11::basic_string, std::allocator >::reserve(unsigned int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/basic_string.tcc line 297
0x401daa46: std::__cxx11::basic_stringbuf, std::allocator >::overflow(int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/sstream.tcc line 114
0x401cd2aa: std::basic_streambuf >::xsputn(char const*, int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/streambuf.tcc line 98
0x401f60d5: std::basic_streambuf >::sputn(char const*, int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/streambuf line 451
0x401d5d61: std::__write(std::ostreambuf_iterator >, char const*, int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/streambuf_iterator.h line 282
0x401d6613: std::num_put > >::_M_insert_int(std::ostreambuf_iterator >, std::ios_base&, char, long) const at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/locale_facets.tcc line 933
0x401d6647: std::num_put > >::do_put(std::ostreambuf_iterator >, std::ios_base&, char, long) const at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/locale_facets.h line 2510
0x401f6ba1: std::num_put > >::put(std::ostreambuf_iterator >, std::ios_base&, char, long) const at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/locale_facets.h line 2371
0x401ce5f9: std::ostream::_M_insert(long) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/ostream.tcc line 73
0x401ce69d: std::ostream::operator(int) at /builds/idf/crosstool-NG/.build/xtensa-esp32-elf/build/build-cc-gcc-final/xtensa-esp32-elf/libstdc++-v3/include/bits/ostream.tcc line 112
0x400d4b06: BLEAddress::toString[abi:cxx11]() at D:\Documents\Arduino\libraries\ESP32_BLE_Arduino\src\BLEAddress.cpp line 87
0x400d713f: BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*) at D:\Documents\Arduino\libraries\ESP32_BLE_Arduino\src\BLEScan.cpp line 101
0x400d605a: BLEDevice::gapEventHandler(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*) at D:\Documents\Arduino\libraries\ESP32_BLE_Arduino\src\BLEDevice.cpp line 272
0x40124c7d: btc_gap_ble_cb_handler at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c line 54
0x4011e642: btc_task at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/bt/common/btc/core/btc_task.c line 163
0x40090962: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

dwaynez
Posts: 2
Joined: Thu Dec 15, 2022 6:48 pm

Re: Abort on BLEAddress tostring

Postby dwaynez » Fri Dec 16, 2022 9:02 pm

I thought the problem might be related to my usage of strings, so I took the away the string usage. The nature of the abort changed, but it is still aborting, I added a display of the heap size in the loop every 10 minutes, but I am not running out of heap (never gets below 62,000 before aborting. I am assuming that either I am getting a malformed scan record or running out of a critical resource. And ides?

New code:

Code: Select all

class MyAdvertisedDeviceCallbacks: public BLEAdvertisedDeviceCallbacks {
    void onResult(BLEAdvertisedDevice advertisedDevice) {
        int rssi = advertisedDevice.getRSSI();
        if (advertisedDevice.haveName() and rssi > RSSI_CUTOFF) {
            char macaddr[17];
            strcpy(macaddr,advertisedDevice.getAddress().toString().c_str());
            if (strstr(allowed_devices,macaddr) != NULL) {
                ble_found = true;
                if (!ble_connected) {
                    ble_connected = true;
                }
            } else if (advertisedDevice.haveManufacturerData()) {
                char manufacturerdata[32];
                strcpy(manufacturerdata,BLEUtils::buildHexData(NULL, (uint8_t*)advertisedDevice.getManufacturerData().data(), advertisedDevice.getManufacturerData().length()));
                if  (strstr(allowed_devices2,manufacturerdata) != NULL) {
                    ble_found = true;
                    if (!ble_connected) {
                        ble_connected = true;
                    }
            }
        } 
    } // onResult
}; // MyAdvertisedDeviceCallbacks
Decoded stack results:

Code: Select all

Decoding stack results
0x4008f670: invoke_abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 156
0x4008f8ed: abort at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/esp32/panic.c line 171
0x401cca03: __cxxabiv1::__terminate(void (*)()) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 47
0x401cca4a: std::terminate() at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_terminate.cc line 57
0x401cc053: __cxxabiv1::__cxa_throw(void*, std::type_info*, void (*)(void*)) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/eh_throw.cc line 87
0x401cc45a: operator new(unsigned int) at /builds/idf/crosstool-NG/.build/src/gcc-5.2.0/libstdc++-v3/libsupc++/new_op.cc line 54
0x400d6f42: BLEScan::handleGAPEvent(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*) at D:\Documents\Arduino\libraries\ESP32_BLE_Arduino\src\BLEScan.cpp line 115
0x400d5dea: BLEDevice::gapEventHandler(esp_gap_ble_cb_event_t, esp_ble_gap_cb_param_t*) at D:\Documents\Arduino\libraries\ESP32_BLE_Arduino\src\BLEDevice.cpp line 272
0x40124a81: btc_gap_ble_cb_handler at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/bt/bluedroid/btc/profile/std/gap/btc_gap_ble.c line 54
0x4011e446: btc_task at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/bt/common/btc/core/btc_task.c line 163
0x40090962: vPortTaskWrapper at /home/runner/work/esp32-arduino-lib-builder/esp32-arduino-lib-builder/esp-idf/components/freertos/port.c line 143

hajo111
Posts: 1
Joined: Sat Nov 25, 2023 8:53 am

Re: Abort on BLEAddress tostring

Postby hajo111 » Sat Nov 25, 2023 9:00 am

Hi dwaynez,

got the same problem. Did you find a solution?

Thanks, Hajo

lbernstone
Posts: 826
Joined: Mon Jul 22, 2019 3:20 pm

Re: Abort on BLEAddress tostring

Postby lbernstone » Sun Nov 26, 2023 4:41 pm

Showing the original error in addition to the stack trace would be useful. Your allocation there is from stack, not heap, so if you are getting a stack canary error, you are running out of stack memory. Check if macaddr is NULL before you copy into it.

Who is online

Users browsing this forum: Majestic-12 [Bot] and 126 guests