1
0
mirror of https://github.com/esphome/esphome.git synced 2025-09-01 10:52:19 +01:00

[api] Fix string lifetime issue in fill_and_encode_entity_info for dynamic object_id

This commit is contained in:
J. Nick Koston
2025-08-28 13:11:58 -05:00
parent 75595b08be
commit 4930027557

View File

@@ -303,11 +303,13 @@ class APIConnection final : public APIServerConnection {
msg.key = entity->get_object_id_hash();
// Try to use static reference first to avoid allocation
StringRef static_ref = entity->get_object_id_ref_for_api_();
// Store dynamic string outside the if-else to maintain lifetime
std::string object_id;
if (!static_ref.empty()) {
msg.set_object_id(static_ref);
} else {
// Dynamic case - need to allocate
std::string object_id = entity->get_object_id();
object_id = entity->get_object_id();
msg.set_object_id(StringRef(object_id));
}