Style might be already inited (potentional memory leak) issue fixed

Hello all,

I just wanted to share my experience with “style might be already inited (potentional memory leak)” issue when using lv_style_init(&style) and how I fixed it.

the issue was recalling lv_style_init() function every time I visit a screen, even if I switch the screen and come back it re initialize the style over and over again which cause holding space in memory, I tried using lv_style_reset but I didn’t work for me, when ever I call it the screen stays white and nothing happens.

the fix is pretty simple, all you need to do is:
1- create global lv_style_t variables
2-create a function that initialize the styles before loading any page( one time run)

for an example read this code:

#include "../../lv_demo.h"

//global style
static lv_style_t containerStyle1;

//call this function before loading any page
void initStyles() {
    lv_style_init(&containerStyle1);

}

lv_obj_t* containerStyle(lv_obj_t* con) {
    lv_color_t darkGreen = lv_color_hsv_to_rgb(182, 86, 11);//Background color

    lv_style_set_bg_color(&containerStyle1, darkGreen);
    lv_style_set_border_opa(&containerStyle1, 0);
    lv_style_set_border_width(&containerStyle1, 2);
    lv_style_set_radius(&containerStyle1, 0);

    lv_obj_add_style(con, &containerStyle1, 0);
    return con;
}

//function that creates a container object
void con (){
    lv_obj_t* con = lv_obj_create(lv_scr_act());
    containerStyle(con);
}

after trying this solution the style might be already inited (potentional memory leak) message disappeared from the log.

Hope that helps you guys.
Peace

Hi,
I’m having the same problems here,
you’ve defined style initialization on a separate function.
Is it OK even though you don’t call that function, initStyles() in anywhere?
I’ve thought I should call it in any function like con(), that make object and create container or screen.

Is it OK even though you don’t call that function, initStyles() in anywhere?

Yes, you just need to call it once and then use the functions for an exmaple lv_obj_t* containerStyle(lv_obj_t* con) to style your objects.

P.S(In style.h write every function you write in style.c to be used in other files)

For an easy explanation for what I did follow these stipes:
1-Create a style.c and style.h files

2-Write the styles you need as a global variable(so it’s won’t keep creating new variables lv_style_t style and does the same issue) for an example static lv_style_t containerStyle1;

3-Write a function that initialize the style you wrote for an example void initStyles()

4-Write a function that has an object parameter and returns an object with the styles you need example lv_obj_t* containerStyle(lv_obj_t* con)

5-include style.h in the first function you’re calling when your code runs( this function should call initStyles() and then the screen you want to show for an exmaple con

6- If you have another screens/functions in different files just include style.h and call the style you want to use for an exmaple containerStyle(object)

hope that answers you question, if you have any other questions just post it and share your code please.