Lv_obj_del(...) and signal LV_SIGNAL_CLEANUP and free allocated variable

Description

I create a object with allocated variable in a obj’s ext_attr.

When the obj is deleted by lv_obj_del(...),
if I want to free this allocated variable automatically
I place free(...) api in LV_SIGNAL_CLEANUP signal .

Is this correct ?
Or is there better method for cleaning up the allocated-variable
when the object is deleted?

Thank you.

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

What do you want to achieve?

What have you tried so far?

Code to reproduce

typedef struct {
  uint8_t* buf;   //  for allocated variable
} lv_mywidget_ext_t;

lv_obj_t* lv_mywidget_create(lv_obj_t* par, lv_obj_t* copy)
{
  ...
     ext->buf = (uint8_t*) lv_mem_alloc( 1024);  //  allocated var.
  ...
}

static lv_res_t lv_mywidget_signal(lv_obj_t * label_grad, lv_signal_t sign, void * param)
{
   ....
   
  if (sign == LV_SIGNAL_CLEANUP) {
    free(ext->buf); ext->buf = NULL;   //  For automatically free, I place this code is right ? 
  }
  return res;
}

Screenshot and/or video

You can use LV_EVENT_DELETE which is more user-friendly.

1 Like

Thank you a lot. :slight_smile:

You’re welcome! :slight_smile: