From e2bab0d492c426a41d8fefff873ffac5479ffcf3 Mon Sep 17 00:00:00 2001 From: cjkas Date: Thu, 22 Oct 2026 20:40:25 +0200 Subject: [PATCH] feat: log failed heap allocations before the abort An allocation that fails inside library code (the web server's operator new) aborts with only a backtrace. A failed-allocation callback now prints the requested size, the caps, the function and what the heap had left (free, largest block, minimum free), using the ROM printf as the logger may need the heap itself. Co-Authored-By: Claude Opus 5.5 --- src/SomfyController.cpp | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/src/SomfyController.cpp b/src/SomfyController.cpp index c6a2ff6..3655b13 100644 --- a/src/SomfyController.cpp +++ b/src/SomfyController.cpp @@ -18,6 +18,9 @@ #include "esp_system.h" #include "esp_app_desc.h" #include "esp_ota_ops.h" +#include "esp_heap_caps.h" +#include "esp_rom_sys.h" +#include #include "Timing.h" static const char *TAG = "Main"; @@ -70,8 +73,18 @@ void listDir(const char *dirname, uint8_t levels) { } } -void setup() { +// A failed allocation in library code aborts with only the backtrace, so this +// leaves the size and what the heap had left next to it. Printed by the ROM +// as the logger might need the heap itself. +static void onAllocFailed(size_t size, uint32_t caps, const char *function) { + esp_rom_printf("Alloc of %u bytes (caps 0x%" PRIx32 ") failed in %s: free %u, largest block %u, min free %u\n", + (unsigned)size, caps, function ? function : "?", (unsigned)heap_caps_get_free_size(caps), + (unsigned)heap_caps_get_largest_free_block(caps), (unsigned)heap_caps_get_minimum_free_size(caps)); +} + +void setup() { Serial.begin(SERIAL_BAUD); + heap_caps_register_failed_alloc_callback(onAllocFailed); ESP_LOGI(TAG, "Startup/Boot...."); esp_core_dump_summary_t summary; if (esp_core_dump_get_summary(&summary) == ESP_OK) {