From 8ddeac1234ce991fb1f34d4b18691bd91e33f33c Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Thu, 29 Jan 2026 17:16:11 +0000 Subject: [PATCH] Fix LVGL object deletion issue in SetChatMessage - Refresh child_count after deleting first_child to avoid using stale count - Add lv_obj_is_valid() checks before accessing deleted objects - Prevent duplicate deletion by refreshing child_count before system message deletion - Ensures last_child validation before scrolling to it Co-authored-by: 78 <4488133+78@users.noreply.github.com> --- main/display/lcd_display.cc | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/main/display/lcd_display.cc b/main/display/lcd_display.cc index c00a8a7d..51ec7d82 100644 --- a/main/display/lcd_display.cc +++ b/main/display/lcd_display.cc @@ -513,19 +513,23 @@ void LcdDisplay::SetChatMessage(const char* role, const char* content) { lv_obj_t* last_child = lv_obj_get_child(content_, child_count - 1); if (first_child != nullptr) { lv_obj_del(first_child); + // Refresh child count after deletion + child_count = lv_obj_get_child_cnt(content_); } // Scroll to the last message immediately - if (last_child != nullptr) { + if (last_child != nullptr && lv_obj_is_valid(last_child)) { lv_obj_scroll_to_view_recursive(last_child, LV_ANIM_OFF); } } // Collapse system messages (if it's a system message, check if the last message is also a system message) if (strcmp(role, "system") == 0) { + // Refresh child count to get accurate count after potential deletion above + child_count = lv_obj_get_child_cnt(content_); if (child_count > 0) { // Get the last message container lv_obj_t* last_container = lv_obj_get_child(content_, child_count - 1); - if (last_container != nullptr && lv_obj_get_child_cnt(last_container) > 0) { + if (last_container != nullptr && lv_obj_is_valid(last_container) && lv_obj_get_child_cnt(last_container) > 0) { // Get the bubble inside the container lv_obj_t* last_bubble = lv_obj_get_child(last_container, 0); if (last_bubble != nullptr) {