Free config items properly on early return

This commit is contained in:
Maschell 2023-11-17 13:50:27 +01:00
parent d2997545fa
commit e5fcf69e88
4 changed files with 56 additions and 40 deletions

View File

@ -4,8 +4,6 @@
#include <cstring> #include <cstring>
#include <wups.h> #include <wups.h>
void WUPSConfigItemBoolean_onDelete(void *context);
int32_t WUPSConfigItemBoolean_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { int32_t WUPSConfigItemBoolean_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
auto *item = (ConfigItemBoolean *) context; auto *item = (ConfigItemBoolean *) context;
snprintf(out_buf, out_size, " %s", item->value ? item->trueValue : item->falseValue); snprintf(out_buf, out_size, " %s", item->value ? item->trueValue : item->falseValue);
@ -58,6 +56,18 @@ void WUPSConfigItemBoolean_restoreDefault(void *context) {
void WUPSConfigItemBoolean_onSelected(void *context, bool isSelected) { void WUPSConfigItemBoolean_onSelected(void *context, bool isSelected) {
} }
static void WUPSConfigItemBoolean_Cleanup(ConfigItemBoolean *item) {
if (!item) {
return;
}
free(item->configId);
free(item);
}
void WUPSConfigItemBoolean_onDelete(void *context) {
WUPSConfigItemBoolean_Cleanup((ConfigItemBoolean *) context);
}
extern "C" bool extern "C" bool
WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback, const char *trueValue, WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback, const char *trueValue,
const char *falseValue) { const char *falseValue) {
@ -92,7 +102,7 @@ WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *
.onDelete = &WUPSConfigItemBoolean_onDelete}; .onDelete = &WUPSConfigItemBoolean_onDelete};
if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) { if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) {
free(item); WUPSConfigItemBoolean_Cleanup(item);
return false; return false;
} }
@ -103,12 +113,6 @@ WUPSConfigItemBoolean_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *
return true; return true;
} }
void WUPSConfigItemBoolean_onDelete(void *context) {
auto *item = (ConfigItemBoolean *) context;
free(item->configId);
free(item);
}
extern "C" bool WUPSConfigItemBoolean_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback) { extern "C" bool WUPSConfigItemBoolean_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName, bool defaultValue, BooleanValueChangedCallback callback) {
return WUPSConfigItemBoolean_AddToCategoryEx(cat, configID, displayName, defaultValue, callback, "true", "false"); return WUPSConfigItemBoolean_AddToCategoryEx(cat, configID, displayName, defaultValue, callback, "true", "false");
} }

View File

@ -59,13 +59,19 @@ void WUPSConfigItemIntegerRange_restoreDefault(void *context) {
item->value = item->defaultValue; item->value = item->defaultValue;
} }
void WUPSConfigItemIntegerRange_onDelete(void *context) { void WUPSConfigItemIntegerRange_onSelected(void *context, bool isSelected) {
auto *item = (ConfigItemIntegerRange *) context; }
static void WUPSConfigItemIntegerRange_Cleanup(ConfigItemIntegerRange *item) {
if (!item) {
return;
}
free(item->configId); free(item->configId);
free(item); free(item);
} }
void WUPSConfigItemIntegerRange_onSelected(void *context, bool isSelected) { void WUPSConfigItemIntegerRange_onDelete(void *context) {
WUPSConfigItemIntegerRange_Cleanup((ConfigItemIntegerRange *) context);
} }
extern "C" bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, int32_t defaultValue, int32_t minValue, int32_t maxValue, extern "C" bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, int32_t defaultValue, int32_t minValue, int32_t maxValue,
@ -101,7 +107,7 @@ extern "C" bool WUPSConfigItemIntegerRange_AddToCategory(WUPSConfigCategoryHandl
.onDelete = &WUPSConfigItemIntegerRange_onDelete}; .onDelete = &WUPSConfigItemIntegerRange_onDelete};
if (WUPSConfigItem_Create(&(item->handle), configId, displayName, callbacks, item) < 0) { if (WUPSConfigItem_Create(&(item->handle), configId, displayName, callbacks, item) < 0) {
free(item); WUPSConfigItemIntegerRange_Cleanup(item);
return false; return false;
}; };

View File

@ -4,8 +4,6 @@
#include <cstring> #include <cstring>
#include <wups.h> #include <wups.h>
void WUPSConfigItemMultipleValues_onDelete(void *context);
int32_t WUPSConfigItemMultipleValues_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { int32_t WUPSConfigItemMultipleValues_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
auto *item = (ConfigItemMultipleValues *) context; auto *item = (ConfigItemMultipleValues *) context;
@ -72,6 +70,27 @@ void WUPSConfigItemMultipleValues_restoreDefault(void *context) {
void WUPSConfigItemMultipleValues_onSelected(void *context, bool isSelected) { void WUPSConfigItemMultipleValues_onSelected(void *context, bool isSelected) {
} }
static void WUPSConfigItemMultipleValues_Cleanup(ConfigItemMultipleValues *item) {
if (!item) {
return;
}
for (int i = 0; i < item->valueCount; ++i) {
free(item->values[i].valueName);
}
free(item->configId);
free(item->values);
free(item);
}
void WUPSConfigItemMultipleValues_onDelete(void *context) {
auto *item = (ConfigItemMultipleValues *) context;
WUPSConfigItemMultipleValues_Cleanup(item);
}
extern "C" bool extern "C" bool
WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName, WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const char *configId, const char *displayName,
int32_t defaultValueIndex, ConfigItemMultipleValuesPair *possibleValues, int32_t defaultValueIndex, ConfigItemMultipleValuesPair *possibleValues,
@ -92,9 +111,7 @@ WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const c
values[i].valueName = nullptr; values[i].valueName = nullptr;
continue; continue;
} }
auto bufLen = strlen(possibleValues[i].valueName) + 1; values[i].valueName = strdup(possibleValues[i].valueName);
values[i].valueName = (char *) malloc(bufLen);
strncpy(values[i].valueName, possibleValues[i].valueName, bufLen);
} }
item->valueCount = pairCount; item->valueCount = pairCount;
@ -120,7 +137,7 @@ WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const c
.onDelete = &WUPSConfigItemMultipleValues_onDelete}; .onDelete = &WUPSConfigItemMultipleValues_onDelete};
if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) { if (WUPSConfigItem_Create(&item->handle, configId, displayName, callbacks, item) < 0) {
free(item); WUPSConfigItemMultipleValues_Cleanup(item);
return false; return false;
} }
@ -129,17 +146,4 @@ WUPSConfigItemMultipleValues_AddToCategory(WUPSConfigCategoryHandle cat, const c
return false; return false;
} }
return true; return true;
}
void WUPSConfigItemMultipleValues_onDelete(void *context) {
auto *item = (ConfigItemMultipleValues *) context;
for (int i = 0; i < item->valueCount; ++i) {
free(item->values[i].valueName);
}
free(item->configId);
free(item->values);
free(item);
} }

View File

@ -4,8 +4,6 @@
#include <cstring> #include <cstring>
#include <wups.h> #include <wups.h>
void WUPSConfigItemStub_onDelete(void *context);
int32_t WUPSConfigItemStub_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) { int32_t WUPSConfigItemStub_getCurrentValueDisplay(void *context, char *out_buf, int32_t out_size) {
memset(out_buf, 0, out_size); memset(out_buf, 0, out_size);
return 0; return 0;
@ -33,6 +31,15 @@ void WUPSConfigItemStub_restoreDefault(void *context) {
void WUPSConfigItemStub_onSelected(void *context, bool isSelected) { void WUPSConfigItemStub_onSelected(void *context, bool isSelected) {
} }
static void WUPSConfigItemStub_Cleanup(ConfigItemStub *item) {
free(item);
}
void WUPSConfigItemStub_onDelete(void *context) {
WUPSConfigItemStub_Cleanup((ConfigItemStub *) context);
}
extern "C" bool extern "C" bool
WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName) { WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName) {
if (cat == 0) { if (cat == 0) {
@ -55,7 +62,7 @@ WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *con
.onDelete = &WUPSConfigItemStub_onDelete}; .onDelete = &WUPSConfigItemStub_onDelete};
if (WUPSConfigItem_Create(&item->handle, configID, displayName, callbacks, item) < 0) { if (WUPSConfigItem_Create(&item->handle, configID, displayName, callbacks, item) < 0) {
free(item); WUPSConfigItemStub_Cleanup(item);
return false; return false;
} }
@ -66,11 +73,6 @@ WUPSConfigItemStub_AddToCategoryEx(WUPSConfigCategoryHandle cat, const char *con
return true; return true;
} }
void WUPSConfigItemStub_onDelete(void *context) {
auto *item = (ConfigItemStub *) context;
free(item);
}
extern "C" bool WUPSConfigItemStub_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName) { extern "C" bool WUPSConfigItemStub_AddToCategory(WUPSConfigCategoryHandle cat, const char *configID, const char *displayName) {
return WUPSConfigItemStub_AddToCategoryEx(cat, configID, displayName); return WUPSConfigItemStub_AddToCategoryEx(cat, configID, displayName);
} }