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 {