通过两个界面切换,验证内存泄漏问题
1.如果是去掉 lv_style_reset(&style); 或者 lv_obj_del(cont2);则会内存漏
2..如果是加上 lv_style_init(&style);和lv_obj_del(cont2);则不会内存漏
3.具体代码如下:
static lv_obj_t *cont1;
static lv_obj_t *cont2;
static void menu1(void);
static void menu2(void);
static void memory_print(void)
{
lv_mem_monitor_t mon;
lv_mem_monitor(&mon);
printf("used: %6d (%3d %%), frag: %3d %%, biggest free: %6d\n", (int)mon.total_size - mon.free_size,
mon.used_pct,
mon.frag_pct,
(int)mon.free_biggest_size);
}
static void obj1_event_hanlder(lv_obj_t *obj,lv_event_t event)
{
switch(event)
{
case LV_EVENT_RELEASED:
printf("cont1 menu\n");
lv_obj_del(cont1);
menu2();
cont1 = NULL;
break;
default:
break;
}
}
static void menu1(void)
{
cont1 = lv_cont_create(lv_scr_act(),NULL);
lv_obj_set_pos(cont1,100,100);
lv_obj_set_size(cont1,400,400);
lv_cont_set_layout(cont1,LV_LAYOUT_OFF);
static lv_style_t style; //样式
lv_style_reset(&style); //重置样式释放内存
lv_style_init(&style);
lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_RED);
lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_add_style(cont2, LV_OBJ_PART_MAIN, &style);
lv_obj_t *cont1_bnt1 = lv_btn_create(cont1,NULL);
lv_obj_t *cont1_btn2 = lv_btn_create(cont1,NULL);
lv_obj_set_pos(cont1_bnt1,100,50);
lv_obj_set_size(cont1_bnt1,100,50);
lv_obj_set_pos(cont1_btn2,210,50);
lv_obj_set_size(cont1_btn2,100,50);
memory_print();
lv_obj_set_event_cb(cont1_bnt1,obj1_event_hanlder);
lv_obj_set_event_cb(cont1_btn2,obj1_event_hanlder);
}
static void obj2_event_hanlder(lv_obj_t *obj,lv_event_t event)
{
switch(event)
{
case LV_EVENT_RELEASED:
printf("cont2 menu\n");
lv_obj_del(cont2);
menu1();
break;
default:
break;
}
}
static void menu2(void)
{
cont2 = lv_cont_create(lv_scr_act(),NULL);
lv_obj_set_pos(cont2,100,100);
lv_obj_set_size(cont2,400,400);
static lv_style_t style; //样式
lv_style_reset(&style); //重置样式释放内存
lv_style_init(&style);
lv_style_set_bg_color(&style, LV_STATE_DEFAULT, LV_COLOR_BLUE);
lv_style_set_text_color(&style, LV_STATE_DEFAULT, LV_COLOR_WHITE);
lv_obj_add_style(cont2, LV_OBJ_PART_MAIN, &style);
lv_obj_t *btn1 = lv_btn_create(cont2,NULL);
lv_obj_align(btn1,NULL,LV_ALIGN_CENTER,0,-80);
lv_btn_set_checkable(btn1,true);
lv_obj_t *btn2 = lv_btn_create(cont2,NULL);
lv_obj_align(btn2,NULL,LV_ALIGN_CENTER,0,80);
memory_print();
lv_obj_set_event_cb(btn1,obj2_event_hanlder);
lv_obj_set_event_cb(btn2,obj2_event_hanlder);
}
void test_start()
{
menu2();
}
int main(int argc, char **argv)
{
(void)argc; /*Unused*/
(void)argv; /*Unused*/
/*Initialize LVGL*/
lv_init();
/*Initialize the HAL (display, input devices, tick) for LVGL*/
hal_init();
test_start();
while (1) {
/* Periodically call the lv_task handler.
* It could be done in a timer interrupt or an OS task too.*/
lv_task_handler();
usleep(5 * 1000);
}
return 0;
}
4.切换两个界面,通过static void memory_print(void)统计剩余内存,
无泄漏情况如下图:
有泄漏情况如下图: