How does object user data works in v8?

Description

I wish to transition to LVGL v8. With versions v6 and v7 I made extensive use of the lv_obj_user_data_t typedef to add my own custom field to lv_obj_t. This has been removed in v8 and replaced with a fixed void * field, which forces me to use dynamic memory to attach custom data to objects. This would be fine if I could also specify how to free said memory when the object is deleted: in previous versions there was a LV_USER_DATA_FREE macro which has been deleted from v8 as well.
What is the prescribed way to sistematically attach custom data to lvgl objects?

One way to accomplish something similar to v7 is to use your own object class. This allows you to effectively specify your own custom structure that extends lv_obj_t. There isn’t really a lot of documentation on this at present, but you can look at a simple widget like the checkbox for an example. As you can see, it uses its own structure (lv_checkbox_t) that includes an lv_obj_t member at the beginning.

Once you create the object using lv_obj_class_create_obj and lv_obj_class_init_obj, you can then cast it to your custom structure and access any extra data you want.

The other option is to use dynamic memory + user data and register an event handler that frees it in LV_EVENT_DELETE.

1 Like

Freeing the data structure on LV_EVENT_DELETE sounds best, I’ll do that. Just to be clear, are there no plans to add some inner mechanism to free a custom data field as in v7?

Most likely not, as the class system is a cleaner option if you want to store more than one value at a time and have it be allocated/freed with the object.

The inner mechanism to free a data field is really LV_EVENT_DELETE.

1 Like