Error:Invalid object

Hellow

Description

I have encounterd a porblem when I am using mask library.
|Error: lv_obj_del |(lv_obj.c #473 lv_obj_del())|
|Error: Invalid object (0x00005648064D6448) |(lv_debug.c #127 lv_debug_log_error())|
my simulation window size 800x480

#define LV_HOR_RES_MAX          (800)
#define LV_VER_RES_MAX          (480)

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

Ubuntu20.04 / Eclipse C/C++

What LVGL version are you using?

7.10.0

What do you want to achieve?

A line which’s color from White to Green to White

What have you tried so far?

I tried 2 canvas,one for color from green to white , another for color from white to green.

Code to reproduce

/**
 * @file test.c
 *
 */

/*********************
 *      INCLUDES
 *********************/
#include "../../lv_examples.h"
#define MASK_WIDTH 800
#define MASK_HEIGHT 10
static uint32_t t;
static lv_obj_t * canvas ;
static lv_obj_t * canvas2 ;
static lv_obj_t * om1 ;
static lv_obj_t * om2 ;
static lv_obj_t * bg1 ;
static lv_obj_t * bg2 ;
static lv_point_t line_points_bg[] = { {0, 5}, {800,5 }};
static lv_opa_t mask_map[MASK_WIDTH * MASK_HEIGHT];
static lv_style_t style_bg1;
static lv_style_t style_bg2;

void aaa();
void plot_(int x);

void test()
{
	//plot_(400);
	aaa();
}

void aaa()
{
	for (int i=0 ;i<=800 ;i=i+10)
	{
		//plot_bar_GtoW(i);
		//plot_bar_WtoG(i);
		plot_(i);
		lv_refr_now(NULL);
	    t = lv_tick_get();
		while(lv_tick_elaps(t) < 2);
	}
}
void plot_(int x)
{
	int y=800-x;
    if (canvas!=NULL)
		lv_obj_del(canvas);
    canvas = lv_canvas_create(lv_scr_act(), NULL);
	lv_obj_align(canvas, NULL, LV_ALIGN_IN_TOP_LEFT, 0, -1);
	lv_canvas_set_buffer(canvas, mask_map, x, MASK_HEIGHT, LV_IMG_CF_ALPHA_8BIT);
	lv_canvas_fill_bg(canvas, LV_COLOR_BLACK, LV_OPA_TRANSP);
    if (canvas2!=NULL)
		lv_obj_del(canvas2);
    canvas2 = lv_canvas_create(lv_scr_act(), NULL);
	lv_obj_align(canvas2, NULL, LV_ALIGN_IN_TOP_RIGHT, 0, -1);
	lv_canvas_set_buffer(canvas2, mask_map, y, MASK_HEIGHT, LV_IMG_CF_ALPHA_8BIT);
	lv_canvas_fill_bg(canvas2, LV_COLOR_BLACK, LV_OPA_TRANSP);

	/*Draw a label to the canvas. The result "image" will be used as mask*/
	lv_draw_line_dsc_t  line_dsc;
	lv_draw_line_dsc_init(&line_dsc);
	line_dsc.color = LV_COLOR_WHITE;
	line_dsc.width = 9;

	//line_points_bg->x=x;
	lv_canvas_draw_line(canvas,line_points_bg,2,&line_dsc);
	lv_canvas_draw_line(canvas2,line_points_bg,2,&line_dsc);

	/*The mask is reads the canvas is not required anymore*/
	lv_obj_del(canvas);
	lv_obj_del(canvas2);

	/*Create an object mask which will use the created mask*/
	om1 = lv_objmask_create(lv_scr_act(), NULL);
	lv_obj_set_size(om1, x, MASK_HEIGHT);
	lv_obj_align(om1, NULL, LV_ALIGN_IN_TOP_LEFT,0 , -1);
	om2 = lv_objmask_create(lv_scr_act(), NULL);
	lv_obj_set_size(om2, y, MASK_HEIGHT);
	lv_obj_align(om2, NULL, LV_ALIGN_IN_TOP_RIGHT,0 , -1);

	lv_draw_mask_map_param_t m1,m2;
	lv_area_t a1,a2;
	a1.x1 = 0;
	a1.y1 = 0;
	a1.x2 = x  -1 ;
	a1.y2 = MASK_HEIGHT-1 ;
	a2.x1 = 0;
	a2.y1 = 0;
	a2.x2 = y  -1 ;
	a2.y2 = MASK_HEIGHT-1 ;

	lv_draw_mask_map_init(&m1, &a1, mask_map);
	lv_objmask_add_mask(om1, &m1);
	lv_draw_mask_map_init(&m2, &a2, mask_map);
	lv_objmask_add_mask(om2, &m2);

    lv_style_init(&style_bg1);
    lv_style_set_bg_opa(&style_bg1, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_bg_color(&style_bg1, LV_STATE_DEFAULT, LV_COLOR_GREEN);
    lv_style_set_bg_grad_color(&style_bg1, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_bg_grad_dir(&style_bg1, LV_STATE_DEFAULT, LV_GRAD_DIR_HOR);
    lv_style_init(&style_bg2);
    lv_style_set_bg_opa(&style_bg2, LV_STATE_DEFAULT, LV_OPA_COVER);
    lv_style_set_bg_color(&style_bg2, LV_STATE_DEFAULT, LV_COLOR_WHITE);
    lv_style_set_bg_grad_color(&style_bg2, LV_STATE_DEFAULT, LV_COLOR_GREEN);
    lv_style_set_bg_grad_dir(&style_bg2, LV_STATE_DEFAULT, LV_GRAD_DIR_HOR);

	bg1 = lv_obj_create(om1, NULL);
	lv_obj_reset_style_list(bg1, LV_OBJ_PART_MAIN);
	lv_obj_add_style(bg1, LV_OBJ_PART_MAIN, &style_bg1);
	lv_obj_set_size(bg1, x, MASK_HEIGHT);
	bg2 = lv_obj_create(om2, NULL);
	lv_obj_reset_style_list(bg2, LV_OBJ_PART_MAIN);
	lv_obj_add_style(bg2, LV_OBJ_PART_MAIN, &style_bg2);
	lv_obj_set_size(bg2, y, MASK_HEIGHT);

}


Screenshot and/or video

Screenshort

You delete the canvas below the /*The mask is reads the canvas is not required anymore*/ but you don’t set the pointers to NULL, so it tries to delete the canvas again using a now outdated pointer.

Hello~
If I set the canvans=NULL,
When I ues this plot function in task, the memory will increase and out of range finally.
How could I free the memory of canvans.
ScreenShot

You still need to delete the canvas. I was pointing out that you retain a now invalid reference after deleting it, so you should set that pointer to NULL once it’s deleted.