Memory leak when delete an copy-object

Hi @kisvegabor , @embeddedt


I want to clone a screen by copy all object from origin-screen
Then I display copy-screen, and change screen from copy-screen to origin-screen by slide animation.
This behavior do again in a cycle of 1 second.
After about 10s (slide 10 times), memory leak and application is stopped.
Maybe there are some data still not freed.
When delete a copy-object which is merely copied from the origin-obj, must I free anything else such as style or user data (style and user data are the data of origin-obj, I think It should not be freed)

What LVGL version are you using?

LVGL ver 7.4.0, ver 7.10.1


lv_obj_t *ori_screen, ori_img_backgound, ori_img_sub;
lv_obj_t *cp_screen, cp_img_background, cp_img_sub;
static lv_style_t style_img_backgound, style_img_sub;

void setup_origin_screen(void)
    //Write codes ori_screen
	ori_screen = lv_obj_create(NULL, NULL);

	//Write codes ori_img_backgound
	ori_img_backgound = lv_img_create(ori_screen, NULL);

	//Write style LV_IMG_PART_MAIN for ori_img_backgound

	//Write style state: LV_STATE_DEFAULT for ori_img_backgound
	lv_style_set_image_recolor(&style_img_backgound, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_image_recolor_opa(&style_img_backgound, LV_STATE_DEFAULT, 0);
	lv_style_set_image_opa(&style_img_backgound, LV_STATE_DEFAULT, 255);
	lv_obj_add_style(ori_img_backgound, LV_IMG_PART_MAIN, &style_img_backgound);
	lv_obj_set_pos(ori_img_backgound, 0, 0);
	lv_obj_set_size(ori_img_backgound, 240, 320);
	lv_obj_set_click(ori_img_backgound, true);
	lv_img_set_pivot(ori_img_backgound, 0,0);
	lv_img_set_angle(ori_img_backgound, 0);

    //Write codes ori_img_sub
	ori_img_sub = lv_img_create(ori_screen, NULL);

	//Write style LV_IMG_PART_MAIN for ori_img_sub

	//Write style state: LV_STATE_DEFAULT for ori_img_sub
	lv_style_set_image_recolor(&style_img_sub, LV_STATE_DEFAULT, lv_color_make(0xff, 0xff, 0xff));
	lv_style_set_image_recolor_opa(&style_img_sub, LV_STATE_DEFAULT, 0);
	lv_style_set_image_opa(&style_img_sub, LV_STATE_DEFAULT, 255);
	lv_obj_add_style(ori_img_sub, LV_IMG_PART_MAIN, &style_img_sub);
	lv_obj_set_pos(ori_img_sub, 0, 0);
	lv_obj_set_size(ori_img_sub, 100, 100);
	lv_obj_set_click(ori_img_sub, true);
	lv_img_set_pivot(ori_img_sub, 0,0);
	lv_img_set_angle(ori_img_sub, 0);

    lv_obj_set_parent(ori_img_sub, ori_img_backgound);
	lv_obj_align(ori_img_sub, ori_img_backgound, LV_ALIGN_CENTER, 0, 0);

void setup_copy_screen(void)
    cp_screen = lv_obj_create(NULL, ori_screen);
    cp_img_background = lv_img_create(cp_screen, ori_img_backgound);
    /* ori_img_sub is child of ori_screen and ori_img_backgound */
    /* So cp_img_sub will be child of both of cp_screen and cp_img_background */
    /* If cp_img_sub is child of cp_img_background, and cp_img_background is child of cp_screen, */
    /* When cp_screen is deleted, does cp_screen recognize cp_img_sub is a child which must be deleted? */
    cp_img_sub = lv_img_create(cp_img_background, ori_img_sub);
void change_copy_to_origin(void)
	lv_scr_load_anim(ori_screen, LV_SCR_LOAD_ANIM_MOVE_RIGHT, 500, 0, true);

void do_cycle(void)

void lvgl_cycle_create(void)
    lv_task_create(do_cycle, 1000, LV_TASK_PRIO_MID, NULL);

The memory leak should be caused by style variable, please call lv_style_reset before lv_style_init and check again.

If I use lv_style_reset for origin-object, the copy-object will be also lost all the style.
I just initial origin-screen and its all style for a one time.
Btw, problem is still not solved by using lv_style_reset .

With the latest v7.11 version (from the release/v7 branch) I don’t see the memory leak.

However, at first try I couldn’t build your example because some *-s were missing. I changed it like this:

lv_obj_t *ori_screen, *ori_img_backgound, *ori_img_sub;
lv_obj_t *cp_screen, *cp_img_background, *cp_img_sub;

Hi @kisvegabor

Sorry, I miss pointer symbol * .
If reduce LV_MEM_SIZE is small enough, memory leak will happen.
And I have fixed it by setting parents of cp_xxx like the ori_xxx
Maybe, cp_screen will not recognize the child of its child. :frowning:

void setup_copy_screen(void)
	cp_screen = lv_obj_create(NULL, ori_screen);
	cp_img_background = lv_img_create(cp_screen, ori_img_backgound);

	/* New Code */
	cp_img_sub = lv_img_create(cp_screen, ori_img_sub);
	lv_obj_set_parent(cp_img_sub, cp_img_background);