Segmentation fault happens when recreate canvas buffer

Important: unclear posts may not receive useful answers.

Before posting

  • Get familiar with Markdown to format and structure your post
  • Be sure to update lvgl from the latest version from the master branch.
  • Be sure you have checked the FAQ and read the relevant part of the documentation.
  • If applicable use the Simulator to eliminate hardware related issues.

Delete this section if you read and applied the mentioned points.

Description

My program may repeatedly enter and exit a new page that needs to use the canvas. In order to save memory, I will free the buffer of the canvas every time I exit. However, I find that sometimes after I re-enter the page and create the canvas and buffer, the image buffer pointer used by lvgl’s drawing task is still the buffer used last time, but it has been freed , so there will be a segment error problem. At first, I thought it was my hardware problem, but I tested on the simulator and meet this problem either. What is the reason? Is there any problem with the way I use canvas? How to solve it?

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

lvgl simulator for windows

What LVGL version are you using?

LVGL-7.4

What do you want to achieve?

delete and recerate canvas and buffer correctly

What have you tried so far?

lv_obj_del(canvas);
free(buf);

Code to reproduce

Add a code snippet which can run in the simulator. It should contain only the relevant code that compiles without errors when separated from your main code base.

The code block(s) should be formatted like:

/*You code here*/

void test_func()
{
lv_obj_t* canvas = NULL;

unsigned char* buf = NULL;

while(1)

{

    buf = (unsigned char*)malloc(LV_CANVAS_BUF_SIZE_TRUE_COLOR(720, 480));

    memset(buf, 0, LV_CANVAS_BUF_SIZE_TRUE_COLOR(720, 480));

    canvas = lv_canvas_create(lv_scr_act(), NULL);

    lv_canvas_set_buffer(canvas, buf, 720, 480, LV_IMG_CF_TRUE_COLOR);

    printf("\n\ncanvas:%#X, buf:%#X\n", canvas, buf);

    sleep(1);

    lv_obj_del(canvas);

    free(buf);

    canvas = NULL;

    buf = NULL;

    sleep(1);

}

}

LV_ATTRIBUTE_FAST_MEM static lv_res_t lv_img_draw_core(const lv_area_t * coords, const lv_area_t * clip_area,

                                                   const void * src,

                                                   const lv_draw_img_dsc_t * draw_dsc)

{

if(draw_dsc->opa <= LV_OPA_MIN) return LV_RES_OK;

lv_img_cache_entry_t * cdsc = _lv_img_cache_open(src, draw_dsc->recolor);

printf("cdsc:%#X\n", cdsc);

printf("img_data:%#X\n", cdsc->dec_dsc.img_data);

if(cdsc == NULL) return LV_RES_INV;

……………………
}

Screenshot and/or video

If possible, add screenshots and/or videos about the current state.
image

Before deleting the canvas try calling lv_img_cache_invalidate_src(lv_canvas_get_img(canvas)).

I suggest updating at least to v7.11. This issue is already fixed there.

Thanks for the answer