How to destroy all screen content to free memory?

My app has 3 “screens”, but can’t keep in memory all at once (only 16K RAM). Is there any simple way to destroy all widgets on screen prior to create new one?

Ideally, i’d like:

  1. Destroy all active screen widgets with allocated memory
  2. Destroy all attached metadata (user data or ext_attr data)

Could not find recommendations in doc about that. Could you advice? I’d like to avoid manual tracking of everything.

UserData should be cleaned/free by yourself first, and then
if you want to clean all child-objs of the current screen, you clean by

lv_obj_clean(lv_scr_act());

https://docs.littlevgl.com/en/html/object-types/obj.html#_CPPv412lv_obj_cleanP8lv_obj_t

2 Likes

Thanks.

I will try to use .ext_attr first, since it’s gc-ed automatically. The only problem is that my app crashes is i try to fill it with data.

You are not allowed to modify the ext_attr pointer directly. That is for internal use by LittlevGL objects. (Although, as mentioned in the other post, you can reallocate it using lv_obj_allocate_ext_attr, and the initial contents will be preserved.)

As a piece of advice, it’s a bad idea to modify any member of the lv_obj_t structure directly (unless we advise you to :slightly_smiling_face:) . Instead, use the available helper functions like lv_obj_set_user_data and lv_obj_get_user_data.

I think a good place to free user data would be in the LV_EVENT_DELETE function.

3 Likes

What do you think about additional flag in lv_obj_set_user_data() call, telling lvgl to free memory automatically? There are many cases, when UserData consists of primitive markers & simple types. It would be convenient to have the same gc as with ext_attr

Sometime user_data is used for other propose, other than about lvgl’s obj.

Such as it has a pointer that pointed to a outside variable.
And this outside variable isn’t a heap-mem allocated variable,
and user doen’t want to be cleaned this data when the obj is deleted.

So lvgl in normally will not do any action about your user_data
when the obj is deleting.

However if any data in your obj’s user_data is any lvgl’s obj
that this data is set to be a children of the obj.
therefore when the obj is deleted,
the child obj ( in your user_data) will be deleted automatically.