Lv_obj_t* as local variable

Hello,

I have button defined as global variable:

lv_obj_t* btn;
btn= lv_btn_create(desk, NULL);

Label on button variable is created in local function:

void aaa()
{
lv_obj_t* label;
label = lv_label_create(btn, NULL);
lv_label_set_text(label, “aaa”);
}

I’m not planning to change labels text. Is it OK to define label as local variable? Should I define it as global?

Hi!
Yes, you can have any lv_* objects as local variables, as:

  1. lv_obj_t* label variable in your aaa() function is just a pointer in stack memory
  2. lv_label_create() function allocates the memory needed for label (in heap), and returns the pointer to that memory space
  3. lv_obj_class.c adds the newly created label (pointer) to parent’s (button) children list, so when the button (or one of it’s parents) will be deleted, then LVGL will implicitly call all children delete function, and memory (in heap) will be deallocated