Change color and font label

Description

how to change the color and font i am using functions for the speed of writing code, but I ran into the problem that if i call the function twice, then the color and font will be with the value of the last

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

esp32

What LVGL version are you using?

What do you want to achieve?

What have you tried so far?

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:

lv_obj_t * label_new(lv_obj_t *cont, char *title, int *x_pos, int *y_pos, const lv_font_t *font_lb, lv_color_t color)									
{
	static lv_style_t style_label_bg;
	lv_style_copy(&style_label_bg, &lv_style_plain);
	style_label_bg.text.color = color;
	style_label_bg.text.font = font_lb;
	
	lv_obj_t *label1 = lv_label_create(cont, NULL);
	lv_label_set_text(label1, title);
	lv_obj_set_pos(label1, x_pos, y_pos);
	lv_label_set_style(label1,LV_LABEL_STYLE_MAIN, &style_label_bg);

	return label1
}

Screenshot and/or video

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

style_label_bg is static, meaning that you are referencing the same style in both calls of the function. You would need to allocate it on the heap using lv_mem_alloc. Depending on whether you are planning to delete the label or not you may need to keep a reference to the style pointer so you can free it later.

In v7 you would be able to just use the local style that every object has on it (you wouldn’t need to create a separate style instance), but there’s no way around that for v6.

i did, only i got a memory leak

lv_obj_t * label_new(lv_obj_t *cont, char *title, int *x_pos, int *y_pos, const lv_font_t *font_lb, lv_color_t color)                                   

{

    lv_style_t *style_lb = lv_mem_alloc(sizeof(lv_style_t));

    lv_style_copy(style_lb, &lv_style_plain);

    style_lb->text.color = color;

    style_lb->text.font = font_lb;

    lv_obj_t *label1 = lv_label_create(cont, NULL);

    lv_label_set_text(label1, title);

    lv_label_set_style(label1, LV_LABEL_STYLE_MAIN, style_lb);

    lv_obj_set_pos(label1, x_pos, y_pos);

    return label1;

}

Sounds like you are creating the label over and over without deleting the previous style. As I mentioned before you will need to keep track of the style reference and free it after deleting the label.

what am I doing wrong?

lv_style_t *style_lb;
lv_obj_t * label_create(lv_obj_t *cont, char *title, int *x_pos, int *y_pos, const lv_font_t *font_lb, lv_color_t color)									// this	function create
{
	style_lb = (lv_style_t*)lv_mem_alloc(sizeof(lv_style_t));
	lv_style_copy(style_lb, &lv_style_plain);
	style_lb->text.color = color;
	style_lb->text.font = font_lb;
	lv_obj_t *label1 = lv_label_create(cont, NULL);
	lv_label_set_text(label1, title);
	lv_label_set_style(label1, LV_LABEL_STYLE_MAIN, style_lb);
	lv_obj_set_pos(label1, x_pos, y_pos);
	return label1;
}

and second function

if(event == LV_EVENT_CLICKED) {
           
			lv_mem_free(style_lb); 
			flag_screen = 25;
			black_screen1();
            lv_obj_del(black_screen);
		}

Freeing the style before deleting the object sounds very suspicious.

nothing changed!

Ok, one thing I forgot: you need to call lv_style_reset(style_lb) before freeing it.

MB it for version v7?
error: implicit declaration of function 'lv_style_reset'; did you mean 'lv_style_init'?

Right; I forgot that you are using v6. You shouldn’t need that for v6 as styles are just a collection of basic variables.

Hmm… maybe you should try commenting out the style_lb related code and see if the memory leak still happens.

no, everything is working correctly
with style_lb

without style_lb

does anyone else have any thoughts?

my decision @embeddedt check please)

int inc_label = 0;
lv_style_t *style_lb[10];
lv_obj_t * label_create(lv_obj_t *cont, char *title, int *x_pos, int *y_pos, const lv_font_t *font_lb, lv_color_t color)									// this	function create
{
	style_lb[inc_label] = (lv_style_t *) malloc(sizeof(lv_style_t));
	lv_style_copy(style_lb[inc_label], &lv_style_plain);
	style_lb[inc_label]->text.color = color;
	style_lb[inc_label]->text.font = font_lb;

	lv_obj_t *label1 = lv_label_create(cont, NULL);
	lv_label_set_text(label1, title);
	lv_label_set_style(label1, LV_LABEL_STYLE_MAIN, style_lb[inc_label]);
	lv_obj_set_pos(label1, x_pos, y_pos);

	inc_label++;
	return label1;
}

and before the place where I call the function

if(inc_label > 0) {
	for(int i=0; i<inc_label; i++) {
		free(style_lb[i]);
	}
	inc_label=0;
}

the problem was that I called the function several times, and cleared the last label, I was inattentive

Glad you figured it out!

You could also use the label’s user data pointer to store the style reference. Either solution works.