diff --git a/esphome/core/helpers.h b/esphome/core/helpers.h index f6942931fb..f1bac3bce1 100644 --- a/esphome/core/helpers.h +++ b/esphome/core/helpers.h @@ -1428,16 +1428,23 @@ template class LazyCallbackManager; * * Memory overhead comparison (32-bit systems): * - CallbackManager: 12 bytes (empty std::vector) - * - LazyCallbackManager: 4 bytes (nullptr unique_ptr) + * - LazyCallbackManager: 4 bytes (nullptr pointer) + * + * Note: Uses plain pointer instead of unique_ptr since callbacks are never freed + * (entities live for device lifetime). This avoids destructor template overhead. * * @tparam Ts The arguments for the callbacks, wrapped in void(). */ template class LazyCallbackManager { public: + /// Destructor - clean up allocated CallbackManager if any. + /// In practice this never runs (entities live for device lifetime) but included for correctness. + ~LazyCallbackManager() { delete this->callbacks_; } + /// Add a callback to the list. Allocates the underlying CallbackManager on first use. void add(std::function &&callback) { if (!this->callbacks_) { - this->callbacks_ = make_unique>(); + this->callbacks_ = new CallbackManager(); } this->callbacks_->add(std::move(callback)); } @@ -1459,7 +1466,7 @@ template class LazyCallbackManager { void operator()(Ts... args) { this->call(args...); } protected: - std::unique_ptr> callbacks_; + CallbackManager *callbacks_{nullptr}; }; /// Helper class to deduplicate items in a series of values.