I have a problem with memory allocation in C and LVGL. The first part is the definitions.
typedef struct
{
unsigned char Widgetcount;
unsigned char index;
lv_obj_t * btn[];
}AssetRADIOBUTTON;
typedef struct{
lv_obj_t * tab;
AssetRADIOBUTTON * Radio1;
}AssetSettingsSome;
typedef struct{
lv_obj_t * ScreenMenuModule;
unsinged char radioCOUNT;
AssetSettingsSome Some;
}GUI_STRUCT_MODULES;
Now for initialization, if I call the memory allocation in subfunction, which works, in the subsubfunction with present code, it doesnt work. Code which works:
void CreateRadioButton(AssetRADIOBUTTON * Radio,lv_obj_t * tab,unsigned char RadioCount)
{
Radio->Widgetcount = RadioCount;
for(unsigned char i=0;i<RadioCount;i++)
Radio->btn[i] = lv_checkbox_create(tab);
Radio->index = 0;
}
void CreateDialog(GUI_STRUCT_MODULES * Settings)
{
Settings->radioCOUNT = 4;
Settings->Some.Radio1 = malloc(sizeof(*Settings->Some.Radio1) + Settings->radioCOUNT * sizeof(*Settings->Some.Radio1->btn));
CreateRadioButton(Settings->Some.Radio1,Settings->ECG.tab,4);
}
void main(void)
{
static GUI_STRUCT_MODULES GUI_MODULES;
CreateDialog(&GUI_MODULES);
}
Code Which doesnt work
void CreateRadioButton(AssetRADIOBUTTON * Radio,lv_obj_t * tab,unsigned char RadioCount)
{
Radio = malloc(sizeof(*Radio) + RadioCount * sizeof(*Radio->btn));
Radio->Widgetcount = RadioCount;
for(unsigned char i=0;i<RadioCount;i++)
Radio->btn[i] = lv_checkbox_create(tab);
Radio->index = 0;
}
void CreateDialog(GUI_STRUCT_MODULES * Settings)
{
CreateRadioButton(Settings->Some.Radio1,Settings->ECG.tab,4);
}
void main(void)
{
static GUI_STRUCT_MODULES GUI_MODULES;
CreateDialog(&GUI_MODULES);
}
Sorry for a bit long MVP.
RadioinCreateRadioButtonis a local variable, and assigning to it has no effect on anything outside the function. So the effect ofCreateRadioButtonis to allocate some memory, write some stuff to it, and then leak it, whileCreateDialogseesSettings->Some.Radio1left unchanged by the call toCreateRadioButton.