Problem with lv_obj_set_user_data & lv_obj_get_user_data

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

I have a function which create for me a label “My_GUI__Label(…)”,
i pass every parameters of my requested label to it, but when user_data ckecked inside the function is correct but outside of function when accesse using
child = lv_obj_get_child(lv_scr_act(), i); user_strR = (char*)lv_obj_get_user_data(child);
the user_data of all objects or some of them THAT created with “My_GUI__Label(…)” goes wrong,
can somebody tell me how i write/access to “My_GUI__Label(…)”,
AND what is going wrong there?!?!

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

Visual Studio 2022 simulator

What LVGL version are you using?

LVGL Ver: 8.3.0 dev

What do you want to achieve?

creating some LVGL object and write/read to USER DATA part from anywhere.

What have you tried so far?

Tried many ways with any success.

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*/


/************************************************************************************************
*
*                         Label Creater
*
*/
lv_obj_t* My_GUI__Label(char* _text, char* _id_strR, lv_obj_t* _parent,
    lv_align_t _Align, uint16_t _X, uint16_t  _Y,
    int width, int height
)
{
    lv_obj_t* label = lv_label_create(_parent);
    lv_label_set_text(label, _text);

    lv_obj_set_pos(label, 700, 100);


    printf("\nMy_GUI__Label  _id_strR  String is:    %s", _id_strR);

    lv_obj_set_user_data(label, (char*) _id_strR);

    return label;
}



/************************************************************************************************
*
*                    Test function to use  Label Creater
*
*/
void My_GUI_test_XX(void)
{
    int   int_desX = 700;
    int   int_desY = 50;
    int   int_width = 20;
    int   int_height = 50;

    char  text_strR[] = "My_GUI_test";
    char append_STR[]  = "My_GUI_test DATA0000000";
    char append_STR1[] = "My_GUI_test DATA1111111";
    char append_STR2[] = "My_GUI_test DATA2222222";
    char append_STR3[] = "My_GUI_test DATA3333333";


    lv_obj_t* lbl1 = My_GUI__Label(text_strR, append_STR, lv_scr_act(),
        NULL, int_desX, int_desY //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );


    lbl1 = My_GUI__Label(text_strR, append_STR1, lv_scr_act(),
        NULL, int_desX + 50, int_desY + 50 //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );


    lbl1 = My_GUI__Label(text_strR, append_STR2, lv_scr_act(),
        NULL, int_desX + 50, int_desY + 50 //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );



    lbl1 = My_GUI__Label(text_strR, append_STR3, lv_scr_act(),
        NULL, int_desX + 50, int_desY + 50 //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );



    lv_obj_t* label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "My_GUI_test_TRADITIONAL");
    lv_obj_set_pos(label, 700, 100);
    lv_obj_set_user_data(label, (void*)"User_Data_My_GUI_test_TRADITIONAL");
    //append_STR = (char*)lv_obj_get_user_data(label);
    //printf("\n++Direct Read______USER data is:    %s", append_STR);

}


/************************************************************************************************
*
*               sample Traditional labels 
*
*/
void Traditional_testGUI(void)
{
    lv_obj_t* button;
    lv_obj_t* label;
    char* got_user_data = NULL;;
    const char* text_strR = NULL;

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "label0");
    lv_obj_set_pos(label, 10, 100);
    lv_obj_set_user_data(label, (void*)"TRADITIONAL Data_0");
    got_user_data = (char*)lv_obj_get_user_data(label);
    printf("\n\n\n\tTraditional_testGUI______USER data is:    %s", got_user_data);


    label = lv_obj_create(lv_scr_act());
    //lv_label_set_text(label, "object0");
    lv_obj_set_pos(label, 10, 300);
    lv_obj_set_user_data(label, (void*)"TRADITIONAL Data_1");
    got_user_data = (char*)lv_obj_get_user_data(label);
    printf("\n\tTraditional_testGUI______USER data is:    %s", got_user_data);//------------------------

    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "label1");
    lv_obj_set_pos(label, 100, 100);
    lv_obj_set_user_data(label, (void*)"TRADITIONAL Data_2");
    got_user_data = (char*)lv_obj_get_user_data(label);
    printf("\n\tTraditional_testGUI______USER data is:    %s", got_user_data);


    label = lv_label_create(lv_scr_act());
    lv_label_set_text(label, "label2");
    lv_obj_set_pos(label, 180, 100);
    lv_obj_set_user_data(label, (void*)"TRADITIONAL Data_3");
    got_user_data = (char*)lv_obj_get_user_data(label);
    printf("\n\tTraditional_testGUI______USER data is:    %s", got_user_data);

    printf("\n\n");

}


/************************************************************************************************
*
*
*
*/
int main()
{
    lv_init();

  

    printf("\n\n----------------------------------");
    printf("\n***   LVGL Ver:   %i.%i.%i %s    ***", LVGL_VERSION_MAJOR, LVGL_VERSION_MINOR, LVGL_VERSION_PATCH, LVGL_VERSION_INFO);
    printf("\n----------------------------------\n\n");


    Traditional_testGUI();



  char* user_strR = NULL;

    printf("\n\n\n----------------- child = lv_obj_get_child(lv_scr_act(), i) -----------------");
    lv_obj_t* child;
    for (int i = 0; i < lv_obj_get_child_cnt(lv_scr_act()); i++) {
        child = lv_obj_get_child(lv_scr_act(), i);

        user_strR = (char*)lv_obj_get_user_data(child);
        if (user_strR != NULL)
            printf("\n++Child find USER data is  i: %i    %s", i, user_strR);
    }


}

Screenshot and/or video

Another try

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

OK
I edited My_GUI_test_XX(void) to Return a pointer to one of the created labels:

lv_obj_t* My_GUI_test_XX(void)
{
    int   int_desX = 400;
    int   int_desY = 50;
    int   int_width = 20;
    int   int_height = 50;

    char* temp_STR;// = "                       ";

    char  text_strR[] = "My_GUI_test";
    char append_STR[]  = "My_GUI_test DATA0000000";
    char append_STR1[] = "My_GUI_test DATA1111111";
    char append_STR2[] = "My_GUI_test DATA2222222";
    char append_STR3[] = "My_GUI_test DATA3333333";

    lv_obj_t* label;

    lv_obj_t* lbl1 = My_GUI__Label(append_STR, append_STR
        , lv_scr_act(),
        NULL, int_desX, int_desY //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );
    temp_STR = (char*)lv_obj_get_user_data(lbl1);
    printf("\n>>>---------------------------- temp_STR  String is READBACK:    %s", temp_STR);



    label = My_GUI__Label(append_STR1, append_STR1
        , lv_scr_act(),
        NULL, int_desX + 50, int_desY + 50 //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );
    temp_STR = (char*)lv_obj_get_user_data(label);
    printf("\n>>>---------------------------- temp_STR  String is READBACK:    %s", temp_STR);



    lbl1 = My_GUI__Label(append_STR2, append_STR2
        , lv_scr_act(),
        NULL, int_desX + 50, int_desY + 50 + 50 //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );
    temp_STR = (char*)lv_obj_get_user_data(lbl1);
    printf("\n>>>---------------------------- temp_STR  String is READBACK:    %s", temp_STR);





    lbl1 = My_GUI__Label(append_STR3, append_STR3
        , lv_scr_act(),
        NULL, int_desX + 50, int_desY + 50 + 50 + 50 //NULL set position 20,20
       // LV_ALIGN_CENTER, 150, -40  //center align
        , int_width, int_height
    );
    temp_STR = (char*)lv_obj_get_user_data(lbl1);
    printf("\n>>>---------------------------- temp_STR  String is READBACK:    %s", temp_STR);





     lbl1 = lv_label_create(lv_scr_act());
    lv_label_set_text(lbl1, "XXXMy_GUI_test_TRADITIONAL");
    lv_obj_set_pos(lbl1, int_desX, int_desY + 50 + 50 + 50+50);
    lv_obj_set_user_data(lbl1, (void*)"User_Data_My_GUI_test_TRADITIONAL");
    //append_STR = (char*)lv_obj_get_user_data(label);
    //printf("\n++Direct Read______USER data is:    %s", append_STR);
    temp_STR = (char*)lv_obj_get_user_data(lbl1);
    printf("\n>>>---------------------------- temp_STR  String is READBACK:    %s", temp_STR);


    return label;

}

and also added some code to main()

    lv_obj_t* label;

    //get access directly to one of objects Directly ((label))
    label = My_GUI_test_XX();
    Traditional_testGUI();



    char* user_strR;

    user_strR = NULL;
    user_strR = (char*)lv_obj_get_user_data(label);    //get access directly to one of objects Directly ((label))
    if (user_strR != NULL)
        printf("\n//get access directly to one of objects  ((label)) >>  USER data is:     %s", user_strR);

So guess what happend, User data is damaged,
Is it a BUG or my code has problem?

Hellooooo… :face_with_raised_eyebrow: :grimacing: :neutral_face:

Hi @sjpw,

I think you should probably declare the following as static or global so they are not destroyed when the stack is reclaimed.

like so:

    static char  text_strR[] = "My_GUI_test";
    static char append_STR[]  = "My_GUI_test DATA0000000";
    static char append_STR1[] = "My_GUI_test DATA1111111";
    static char append_STR2[] = "My_GUI_test DATA2222222";
    static char append_STR3[] = "My_GUI_test DATA3333333";

Hope that helps.

Kind Regards,

Pete

1 Like

Thank you.

1 Like