From 00e128bdd5e1dd0c725a8b334d6168c82a1cd9de Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Thu, 8 May 2025 15:29:11 -0500 Subject: [PATCH] Fix heap tracing function scope issues - Add extern \C\ linkage to heap tracing functions - Forward declare the functions in the API server implementation - Ensures the heap tracing functions are accessible from any namespace --- esphome/components/api/__init__.py | 7 ++++--- esphome/components/api/api_server.cpp | 8 ++++++++ 2 files changed, 12 insertions(+), 3 deletions(-) diff --git a/esphome/components/api/__init__.py b/esphome/components/api/__init__.py index ae621d4802..9a2078f37b 100644 --- a/esphome/components/api/__init__.py +++ b/esphome/components/api/__init__.py @@ -221,18 +221,19 @@ async def to_code(config): ) ) - # Add helper functions for heap tracing + # Add helper functions for heap tracing with extern "C" to make them globally accessible cg.add_global( cg.RawStatement( """ -void start_heap_trace() { +// Global heap tracing functions that can be called from any context +extern "C" void start_heap_trace() { heap_trace_init_standalone(trace_record, """ + str(num_records) + """); heap_trace_start(HEAP_TRACE_LEAKS); } -void stop_and_dump_heap_trace() { +extern "C" void stop_and_dump_heap_trace() { heap_trace_stop(); heap_trace_dump(); } diff --git a/esphome/components/api/api_server.cpp b/esphome/components/api/api_server.cpp index 830b0acce1..0d03f15211 100644 --- a/esphome/components/api/api_server.cpp +++ b/esphome/components/api/api_server.cpp @@ -14,6 +14,14 @@ #include "esphome/components/logger/logger.h" #endif +#ifdef USE_API_HEAP_TRACE +#include "esp_heap_trace.h" + +// Forward declare heap tracing functions that will be used in the API class +extern "C" void start_heap_trace(); +extern "C" void stop_and_dump_heap_trace(); +#endif + #include namespace esphome {