Delete a task to avoid segmentation fault

Dear all,

I did not find an alternative function in python for lv_task_del() of C.

When using C language.
I tried to use lv_obj_del(lv_scr_act()) to clear all widgets in a screen but it caused an exception “Segmentation fault” when the widget is using a task to animate. Therefore, before clear widgets, I called lv_task_del() to remove the active animation task and it worked well.

When using Python.
I tried to use anim_task=None or del anim_task to remove the animation task in Micropython but they did not avoid the “Segmentation fault”.

Could you tell me the way to clear all widgets from a screen that runs animation in Micropython?

Hi @keisak,
Which version of LVGL are you using?

del is a reserved Python word.
It is replaced by _del (or delete on older versions).
Please try using task._del

@kisvegabor @embeddedt - Is it Illegal to call lv_obj_del(lv_scr_act())?

Yes, LVGL needs a screen to be active.

The safe alternative is lv_obj_clean(lv_scr_act()), which will remove all objects on the screen without deleting the screen itself.

Hi @amirgon,
Thank you so much.

amirgon:
Which version of LVGL are you using?

lvgl/lvgl_micropython ver1.14
lvgl 7.10.1

@amirgon:
del is a reserved Python word.
It is replaced by _del (or delete on older versions).
Please try using task._del

I tried to use task._del() as well but it could not avoid the exception.

@embeddedt
The safe alternative is lv_obj_clean(lv_scr_act()) , which will remove all objects on the screen without deleting the screen itself.

I also tried to use " lv_obj_clean(lv_scr_act()) but it caused the same exception so I suppose that deleting animation task and objects such as task._del() are needed in advance.
In C language code, lv_task_del(task) works but in micropython code, task._del() seems not to work.

More details

I am evaluating to switch from an application to another application.
At first I wrote some applications using LVGL in C language.

  1. using a sample of lv_demo_widgets.c, I ran lv_demo_widgets().
  2. The demo showed widgets on a screen and all widgets work fine interacting user actions .
  3. I added a feature to switch to another application by pressing a key as follows.
  4. When the specific key is pressed, lv_obj_clean(lv_scr_act()) is called to clean all widgets on the screen before running another application.
  5. But it caused an exception with Segmentation fault in the animation task procedure.
  6. I supposed that the animation task related to the deleted widgets is still alive. it caused the error since it lost the widget. 7. Therefore, I called lv_task_del() to remove the active animation task before calling v_obj_clean(lv_scr_act()).
  7. It worked fine. The exception never occurred in the C language environment.

Next, I tried to implement the same process into the python environment.

  1. using a sample of lv_demo_widgets.py, import this module and run lv_demo_widgets().
  2. The demo showed widgets on a screen and all widgets work fine interacting user actions .
  3. When the specific key is pressed, it runs a method that calls task._del() before calling the lv_obj_clean(lv_scr_act()) before running another python script.
  4. But it caused an exception with Segmentation fault in the animation task procedure despite the calling task._del().

I suppose that animation objects such as a_arc, a_ga, a_lm are still alive and they link each widget arc, gauge and linearmeter that are removed. Then the exception occurs.

I should delete the animation objects before clear screen. How can I stop the animation objects and removed.

the place where an exception with Segmentation fault occurred

After calling lv_obj_clean(lv_scr_act()),
An exception has occurred at

 2139 lv_obj_t * lv_obj_get_parent(const lv_obj_t * obj)
 2140 {
 2141    LV_ASSERT_OBJ(obj, LV_OBJX_NAME);
 2142
 2144    return obj->parent;   ← here
 2144 }

in at 477 line in lv_anim.c

 438 static void anim_task(lv_task_t * param)
 439 {
  …
  474    if(new_value != a->current) {
  475          a->current = new_value;
  476         /*Apply the calculated value*/
  477         if(a->exec_cb) a->exec_cb(a->var, new_value);  ← in this function

You first need to stop animations before deleting an animated object.
In Micropython you can use anim.custom_del(anim_func) to stop the animation, only then delete the object.

If this still doesn’t work, please provide a short but complete example so I could reproduce the problem on my side .

I’ve fixed the issue with it.
Thank you very much for the help provided.