Properly release memory on partially read configuration

This commit is contained in:
Benny Baumann 2021-10-06 00:00:13 +02:00 committed by BenBE
parent 8ac8542b6e
commit 2a13e15ad0
1 changed files with 14 additions and 6 deletions

View File

@ -28,11 +28,7 @@ void Settings_delete(Settings* this) {
free(this->filename); free(this->filename);
free(this->fields); free(this->fields);
for (unsigned int i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) { for (unsigned int i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) {
if (this->hColumns[i].names) { String_freeArray(this->hColumns[i].names);
for (size_t j = 0; j < this->hColumns[i].len; j++)
free(this->hColumns[i].names[j]);
free(this->hColumns[i].names);
}
free(this->hColumns[i].modes); free(this->hColumns[i].modes);
} }
free(this->hColumns); free(this->hColumns);
@ -67,14 +63,26 @@ static void Settings_readMeterModes(Settings* this, const char* line, unsigned i
static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount) { static void Settings_defaultMeters(Settings* this, unsigned int initialCpuCount) {
int sizes[] = { 3, 3 }; int sizes[] = { 3, 3 };
if (initialCpuCount > 4 && initialCpuCount <= 128) { if (initialCpuCount > 4 && initialCpuCount <= 128) {
sizes[1]++; sizes[1]++;
} }
for (int i = 0; i < 2; i++) {
// Release any previously allocated memory
for (size_t i = 0; i < HeaderLayout_getColumns(this->hLayout); i++) {
String_freeArray(this->hColumns[i].names);
free(this->hColumns[i].modes);
}
free(this->hColumns);
this->hLayout = HF_TWO_50_50;
this->hColumns = xCalloc(HeaderLayout_getColumns(this->hLayout), sizeof(MeterColumnSetting));
for (size_t i = 0; i < 2; i++) {
this->hColumns[i].names = xCalloc(sizes[i] + 1, sizeof(char*)); this->hColumns[i].names = xCalloc(sizes[i] + 1, sizeof(char*));
this->hColumns[i].modes = xCalloc(sizes[i], sizeof(int)); this->hColumns[i].modes = xCalloc(sizes[i], sizeof(int));
this->hColumns[i].len = sizes[i]; this->hColumns[i].len = sizes[i];
} }
int r = 0; int r = 0;
if (initialCpuCount > 128) { if (initialCpuCount > 128) {