Lv_table_create issue when copy param is given


When creating a table from an existing one an access violation occurs at refresh.

It looks to me that the cause is ext->cell_data is not being copied from copy_ext->cell_data which would involve among other things allocating memory for the contents.and copying it to the allocated memory from the respective locations…

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


What do you experience?

Exception thrown at 0x00007FF6F6D3DE72 in visual_studio_2017_sdl_x64.exe: 0xC0000005: Access violation reading location 0x0000000000000000.

What do you expect?

Just letting you know

Code to reproduce

From lv_ex_table_1, add last two lines.

void lv_ex_table_1(void)
    /*Create a normal cell style*/
    static lv_style_t style_cell1;
    lv_style_copy(&style_cell1, &lv_style_plain);
    style_cell1.body.border.width = 1;
    style_cell1.body.border.color = LV_COLOR_BLACK;

    /*Crealte a header cell style*/
    static lv_style_t style_cell2;
    lv_style_copy(&style_cell2, &lv_style_plain);
    style_cell2.body.border.width = 1;
    style_cell2.body.border.color = LV_COLOR_BLACK;
    style_cell2.body.main_color = LV_COLOR_SILVER;
    style_cell2.body.grad_color = LV_COLOR_SILVER;

    lv_obj_t * table = lv_table_create(lv_scr_act(), NULL);
    lv_table_set_style(table, LV_TABLE_STYLE_CELL1, &style_cell1);
    lv_table_set_style(table, LV_TABLE_STYLE_CELL2, &style_cell2);
    lv_table_set_style(table, LV_TABLE_STYLE_BG, &lv_style_transp_tight);
    lv_table_set_col_cnt(table, 2);
    lv_table_set_row_cnt(table, 4);
    lv_obj_align(table, NULL, LV_ALIGN_CENTER, 0, 0);

    /*Make the cells of the first row center aligned */
    lv_table_set_cell_align(table, 0, 0, LV_LABEL_ALIGN_CENTER);
    lv_table_set_cell_align(table, 0, 1, LV_LABEL_ALIGN_CENTER);

    /*Make the cells of the first row TYPE = 2 (use `style_cell2`) */
    lv_table_set_cell_type(table, 0, 0, 2);
    lv_table_set_cell_type(table, 0, 1, 2);

    /*Fill the first column*/
    lv_table_set_cell_value(table, 0, 0, "Name");
    lv_table_set_cell_value(table, 1, 0, "Apple");
    lv_table_set_cell_value(table, 2, 0, "Banana");
    lv_table_set_cell_value(table, 3, 0, "Citron");

    /*Fill the second column*/
    lv_table_set_cell_value(table, 0, 1, "Price");
    lv_table_set_cell_value(table, 1, 1, "$7");
    lv_table_set_cell_value(table, 2, 1, "$4");
    lv_table_set_cell_value(table, 3, 1, "$6");

    lv_obj_t* table2 = lv_table_create(lv_scr_act(), table);
    lv_obj_align(table2, table, LV_ALIGN_OUT_BOTTOM_MID, 0, LV_DPI/4);

Will fix this when I get a chance.

I’ve added a simple fix to avoid crashing.

It doesn’t copy the content of the table now. I’m not sure this the right approach but I think usually it’s pointless to have two tables with the same content. Therefore it copies only the “shape” of the table.

What do you think?

Thank you,
For me that is fine, the main thing I wanted to do was to set up the styles with one table (and then agregate to other tables I needed) and not necesarily the content.
All I did was the following as they they have already been been set to 0 above;

//    ext->col_cnt = copy_ext->col_cnt;
//    ext->row_cnt = copy_ext->row_cnt;

Your fix is better.

Another question,
In order to copy, should the table style not also be initialised ?

    new_table->style_p = copy->style_p;


    lv_table_set_style(new_table, LV_TABLE_STYLE_BG, copy->style_p);

The background style is copied in lv_obj_create and cell styles are explicitly copied. Or is not working properly?

Sorry, my mistake. I did not notice that copy was passed to lv_obj_create.

To backtrack a little,

IMHO, In addition to table dimension it would be useful if attributes (other than contents) were reproduced to new_table. i.e. those set by lv_table_set_cell_type, lv_table_set_cell_align, lv_table_set_col_width etc.
IMV, That way the copy behaviour would more consistent when compared to that of other objects.

Would it be feasible (as a future feature) to extend the API to provide for lv_table_set_cell_static_value, that would be analogous to lv_label_set_static_text in the label context ?

Thanks again !

No problem :slight_smile:

Good idea!

Now the cell metadata (align, type, etc) is stored in the first byte of the cell’s text. It would be difficult to handle static text and still use the same method to describe the metadata. See this: