Memory Leak during lv_style_set_bg_color()?

Description

What MCU/Processor/Board and compiler are you using?

What do you experience?

I extended the demo example to show a modal dialog for 1 sec and then destroy it again, and after 1 sec show again…
During that I monitor the memory usage.

You can see that with every iteration the memory decreases and the used_cnd increases:

I (1690) demo: memory: total 32768, free 29428, free_biggest 29420, used_cnd 67, used_pct 11, frag_pct 1
I (2700) demo: memory: total 32768, free 29652, free_biggest 29620, used_cnd 62, used_pct 10, frag_pct 1
I (3710) demo: memory: total 32768, free 29412, free_biggest 29408, used_cnd 68, used_pct 11, frag_pct 1
I (4720) demo: memory: total 32768, free 29632, free_biggest 29620, used_cnd 63, used_pct 10, frag_pct 1
I (5730) demo: memory: total 32768, free 29396, free_biggest 29392, used_cnd 69, used_pct 11, frag_pct 1
I (6740) demo: memory: total 32768, free 29616, free_biggest 29604, used_cnd 64, used_pct 10, frag_pct 1
I (7750) demo: memory: total 32768, free 29380, free_biggest 29376, used_cnd 70, used_pct 11, frag_pct 1
I (8760) demo: memory: total 32768, free 29600, free_biggest 29588, used_cnd 65, used_pct 10, frag_pct 1

I found that if I comment out the lines for setting the color, everything is fine. Remove:

lv_style_set_bg_color(&modal_style, LV_STATE_DEFAULT, LV_COLOR_LIME);
lv_style_set_bg_grad_color(&modal_style, LV_STATE_DEFAULT, LV_COLOR_LIME);

and you get, which is good:

I (1700) demo: memory: total 32768, free 29440, free_biggest 29432, used_cnd 66, used_pct 11, frag_pct 1
I (2710) demo: memory: total 32768, free 29664, free_biggest 29620, used_cnd 61, used_pct 10, frag_pct 1
I (3720) demo: memory: total 32768, free 29440, free_biggest 29432, used_cnd 66, used_pct 11, frag_pct 1
I (4730) demo: memory: total 32768, free 29664, free_biggest 29620, used_cnd 61, used_pct 10, frag_pct 1
I (5740) demo: memory: total 32768, free 29440, free_biggest 29432, used_cnd 66, used_pct 11, frag_pct 1
I (6750) demo: memory: total 32768, free 29664, free_biggest 29620, used_cnd 61, used_pct 10, frag_pct 1

What do you expect?

no memory leak even with setting the colors.

Code to reproduce

This function is called every 1 sec. The complete code is available here.

void update_task(lv_task_t * task)
{
    static bool show_modal_dialog = false;
    static lv_style_t modal_style;
    static lv_obj_t *mbox_obj;
    static lv_obj_t *text;

    show_modal_dialog = (show_modal_dialog ? false : true);

    if(show_modal_dialog){
        lv_style_init(&modal_style);
        lv_style_set_bg_color(&modal_style, LV_STATE_DEFAULT, LV_COLOR_LIME);
        lv_style_set_bg_grad_color(&modal_style, LV_STATE_DEFAULT, LV_COLOR_LIME);
    
        mbox_obj = lv_obj_create(lv_scr_act(), NULL);
        lv_obj_add_style(mbox_obj, LV_OBJ_PART_MAIN, &modal_style);
        lv_obj_set_pos(mbox_obj, 0, 0);
        lv_obj_set_size(mbox_obj, LV_HOR_RES-16, LV_VER_RES-8);
        lv_obj_align(mbox_obj, NULL, LV_ALIGN_CENTER, 0, 0);

        text = lv_label_create(mbox_obj, NULL);
        lv_label_set_text(text, "Text");
        lv_obj_align(text, NULL, LV_ALIGN_CENTER, 0, 0);
    } else {
        lv_obj_del(mbox_obj);
    }
    print_memory();
}

It’s because you’re reinitializing the style each time. You only need to initialize it once.

Ok, understood. Is there a way to de-init a style?

You can use lv_style_reset (it’s mentioned at the bottom of that section of the documentation).

1 Like

Thanks!
If I initialize only once, everything is fine. So we can close this topic.