LVGL 8.3 version, deleting the chart control involves memory leaks

The chart control is set to LV_CHART_TYPE_SCATTER mode. After deleting the chart control, it is found that the memory allocated to the x-axis is not released.

What type of memory management did you select? By LVGL or by c compiler free and malloc function?

I am using LVGL’s memory management function. Comparing with LVGL 9.3, I found that the memory allocated for the X axis was not released in the lv_chart_destructor function. I modified it according to the 9.3 version, but it did not really solve the problem.


I have been repeatedly creating chart controls and then deleting them. The type of the first chart control created is LV_CHART_TYPE_LINE. After deleting the current chart control, the type of the second chart control created is LV_CHART_TYPE_SCATTER. After deleting the current chart control, the memory of the X axis will not be released.

Hi,

Opps, it’s really a bug in LVGL. However by adding

        if(!ser->x_ext_buf_assigned) lv_mem_free(ser->x_points);

I could resolve it. I’ve tested with:

  for(int i = 0; i < 100; i++) {
	  lv_example_chart_7();
	  lv_obj_clean(lv_scr_act());
  }

Thank you very much for your reply. I have tried to add if(!ser->x_ext_buf_assigned) lv_mem_free(ser->x_points) to solve the problem, but it does not completely solve the problem. You can do the following test. First create a chart control, set the mode to LV_CHART_TYPE_LINE, and then delete the chart control. Then create another chart control, set the mode to LV_CHART_TYPE_SCATTER, use the lv_chart_set_point_count(ui_Chart1,600) function to allocate more memory space for the X axis, and then delete the chart control. Then repeat this process. The test found that sometimes the X axis memory release fails.

Please send a small code snippet to save some iterations :slight_smile:

Thank you very much for your reply. I made a simple test program. You can keep clicking the button to reproduce the problem.
test_demo.txt (1.2 KB)

Thank you! Could you try with this patch?

diff --git a/src/extra/widgets/chart/lv_chart.c b/src/extra/widgets/chart/lv_chart.c
index 0c8aeefe6..19c74a910 100644
--- a/src/extra/widgets/chart/lv_chart.c
+++ b/src/extra/widgets/chart/lv_chart.c
@@ -348,6 +348,7 @@ lv_chart_series_t * lv_chart_add_series(lv_obj_t * obj, lv_color_t color, lv_cha
     lv_chart_series_t * ser = _lv_ll_ins_head(&chart->series_ll);
     LV_ASSERT_MALLOC(ser);
     if(ser == NULL) return NULL;
+    lv_memset_00(ser, sizeof(lv_chart_series_t));
 
     lv_coord_t def = LV_CHART_POINT_NONE;
 
@@ -365,9 +366,6 @@ lv_chart_series_t * lv_chart_add_series(lv_obj_t * obj, lv_color_t color, lv_cha
         return NULL;
     }
 
-    ser->start_point = 0;
-    ser->y_ext_buf_assigned = false;
-    ser->hidden = 0;
     ser->x_axis_sec = axis & LV_CHART_AXIS_SECONDARY_X ? 1 : 0;
     ser->y_axis_sec = axis & LV_CHART_AXIS_SECONDARY_Y ? 1 : 0;
 
@@ -681,6 +679,7 @@ static void lv_chart_destructor(const lv_obj_class_t * class_p, lv_obj_t * obj)
         ser = _lv_ll_get_head(&chart->series_ll);
 
         if(!ser->y_ext_buf_assigned) lv_mem_free(ser->y_points);
+        if(!ser->x_ext_buf_assigned && ser->x_points) lv_mem_free(ser->x_points);
 
         _lv_ll_remove(&chart->series_ll, ser);
         lv_mem_free(ser);

Thank you. The problem is solved

Great, Pull request opened: fix(chart): fix memory leak with scatter charts by kisvegabor · Pull Request #7096 · lvgl/lvgl · GitHub