From bb4ac69d336c30741248ee82b93faee4952cd4d3 Mon Sep 17 00:00:00 2001 From: "Guilherme G. Menaldo" Date: Wed, 10 Apr 2024 22:03:53 -0300 Subject: [PATCH] Fix memory leak when reloading unit params db --- src/map/status.c | 33 ++++++++++----- src/map/status.h | 3 +- src/plugins/HPMHooking/HPMHooking.Defs.inc | 6 ++- .../HPMHooking_map.HPMHooksCore.inc | 12 ++++-- .../HPMHooking_map.HookingPoints.inc | 3 +- .../HPMHooking/HPMHooking_map.Hooks.inc | 42 +++++++++++++++---- 6 files changed, 72 insertions(+), 27 deletions(-) diff --git a/src/map/status.c b/src/map/status.c index 476032fb30f..a278a1b240f 100644 --- a/src/map/status.c +++ b/src/map/status.c @@ -14223,7 +14223,7 @@ static bool status_read_unit_params_db_sub(const char *name, struct config_setti safestrncpy(entry.name, name, sizeof(entry.name)); if (!status->read_unit_params_db_maxhp(&entry, inherited, group, source)) { - status->unit_params_destroy(&entry); + status->unit_params_destroy_entry(&entry); return false; } @@ -14278,7 +14278,7 @@ static bool status_read_unit_params_db_sub(const char *name, struct config_setti } if (!status->read_unit_params_db_additional(&entry, inherited, group, source)) { - status->unit_params_destroy(&entry); + status->unit_params_destroy_entry(&entry); return false; } @@ -14293,8 +14293,6 @@ static bool status_read_unit_params_db_sub(const char *name, struct config_setti */ static void status_read_unit_params_db(void) { - VECTOR_INIT(status->unit_params_groups); - char config_filename[256]; libconfig->format_db_path(DBPATH"unit_parameters_db.conf", config_filename, sizeof(config_filename)); @@ -14323,7 +14321,7 @@ static void status_read_unit_params_db(void) * Perform the required cleanup inside a unit parameters db entry. * @param entry the entry to have its internal content cleared */ -static void status_unit_params_destroy(struct s_unit_params *entry) +static void status_unit_params_destroy_entry(struct s_unit_params *entry) { nullpo_retv(entry); @@ -14334,6 +14332,17 @@ static void status_unit_params_destroy(struct s_unit_params *entry) } } +/** + * Perform the required cleanup of the unit parameters db + */ +static void status_unit_params_clear_db(void) +{ + for (int i = 0; i < VECTOR_LENGTH(status->unit_params_groups); ++i) + status->unit_params_destroy_entry(&VECTOR_INDEX(status->unit_params_groups, i)); + + VECTOR_CLEAR(status->unit_params_groups); +} + /** * Read status db * job1.txt @@ -14355,6 +14364,8 @@ static int status_readdb(void) memset(status->dbs->SP_table, 0, sizeof(status->dbs->SP_table)); // reset job_db2.txt data memset(status->dbs->job_bonus,0,sizeof(status->dbs->job_bonus)); // Job-specific stats bonus + // resets unit_params_db.conf data + status->unit_params_clear_db(); } for ( i = 0; i < CLASS_COUNT; i++ ) { for ( j = 0; j < MAX_SINGLE_WEAPON_TYPE; j++ ) @@ -14390,6 +14401,8 @@ static int do_init_status(bool minimal) if (minimal) return 0; + VECTOR_INIT(status->unit_params_groups); + timer->add_func_list(status->change_timer,"status_change_timer"); timer->add_func_list(status->kaahi_heal_timer,"status_kaahi_heal_timer"); timer->add_func_list(status->natural_heal_timer,"status_natural_heal_timer"); @@ -14406,11 +14419,8 @@ static void do_final_status(void) { ers_destroy(status->data_ers); - status->unit_params_destroy(&status->dummy_unit_params); - for (int i = 0; i < VECTOR_LENGTH(status->unit_params_groups); ++i) - status->unit_params_destroy(&VECTOR_INDEX(status->unit_params_groups, i)); - - VECTOR_CLEAR(status->unit_params_groups); + status->unit_params_destroy_entry(&status->dummy_unit_params); + status->unit_params_clear_db(); } /*===================================== @@ -14595,7 +14605,8 @@ void status_defaults(void) status->maxhp_entry_compare = status_maxhp_entry_compare; status->read_unit_params_db_maxhp = status_read_unit_params_db_maxhp; status->read_unit_params_db_additional = status_read_unit_params_db_additional; - status->unit_params_destroy = status_unit_params_destroy; + status->unit_params_destroy_entry = status_unit_params_destroy_entry; + status->unit_params_clear_db = status_unit_params_clear_db; status->copy = status_copy; status->base_matk_min = status_base_matk_min; status->base_matk_max = status_base_matk_max; diff --git a/src/map/status.h b/src/map/status.h index 4cc0fa839e7..4944d03bd42 100644 --- a/src/map/status.h +++ b/src/map/status.h @@ -1488,7 +1488,8 @@ struct status_interface { bool (*read_unit_params_db_maxhp) (struct s_unit_params *entry, struct s_unit_params *inherited, struct config_setting_t *group, const char *source); int (*maxhp_entry_compare) (const void *entry1, const void *entry2); bool (*read_unit_params_db_additional) (struct s_unit_params *entry, struct s_unit_params *inherited, struct config_setting_t *group, const char *source); - void (*unit_params_destroy) (struct s_unit_params *entry); + void (*unit_params_destroy_entry) (struct s_unit_params *entry); + void (*unit_params_clear_db) (void); void (*copy) (struct status_data *a, const struct status_data *b); int (*base_matk_min) (const struct status_data *st); int (*base_matk_max) (const struct status_data *st); diff --git a/src/plugins/HPMHooking/HPMHooking.Defs.inc b/src/plugins/HPMHooking/HPMHooking.Defs.inc index bdecd0f8f41..e0ce9952a16 100644 --- a/src/plugins/HPMHooking/HPMHooking.Defs.inc +++ b/src/plugins/HPMHooking/HPMHooking.Defs.inc @@ -9312,8 +9312,10 @@ typedef int (*HPMHOOK_pre_status_maxhp_entry_compare) (const void **entry1, cons typedef int (*HPMHOOK_post_status_maxhp_entry_compare) (int retVal___, const void *entry1, const void *entry2); typedef bool (*HPMHOOK_pre_status_read_unit_params_db_additional) (struct s_unit_params **entry, struct s_unit_params **inherited, struct config_setting_t **group, const char **source); typedef bool (*HPMHOOK_post_status_read_unit_params_db_additional) (bool retVal___, struct s_unit_params *entry, struct s_unit_params *inherited, struct config_setting_t *group, const char *source); -typedef void (*HPMHOOK_pre_status_unit_params_destroy) (struct s_unit_params **entry); -typedef void (*HPMHOOK_post_status_unit_params_destroy) (struct s_unit_params *entry); +typedef void (*HPMHOOK_pre_status_unit_params_destroy_entry) (struct s_unit_params **entry); +typedef void (*HPMHOOK_post_status_unit_params_destroy_entry) (struct s_unit_params *entry); +typedef void (*HPMHOOK_pre_status_unit_params_clear_db) (void); +typedef void (*HPMHOOK_post_status_unit_params_clear_db) (void); typedef void (*HPMHOOK_pre_status_copy) (struct status_data **a, const struct status_data **b); typedef void (*HPMHOOK_post_status_copy) (struct status_data *a, const struct status_data *b); typedef int (*HPMHOOK_pre_status_base_matk_min) (const struct status_data **st); diff --git a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc index 4e3b66ea5a3..3d155184784 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc @@ -7228,8 +7228,10 @@ struct { struct HPMHookPoint *HP_status_maxhp_entry_compare_post; struct HPMHookPoint *HP_status_read_unit_params_db_additional_pre; struct HPMHookPoint *HP_status_read_unit_params_db_additional_post; - struct HPMHookPoint *HP_status_unit_params_destroy_pre; - struct HPMHookPoint *HP_status_unit_params_destroy_post; + struct HPMHookPoint *HP_status_unit_params_destroy_entry_pre; + struct HPMHookPoint *HP_status_unit_params_destroy_entry_post; + struct HPMHookPoint *HP_status_unit_params_clear_db_pre; + struct HPMHookPoint *HP_status_unit_params_clear_db_post; struct HPMHookPoint *HP_status_copy_pre; struct HPMHookPoint *HP_status_copy_post; struct HPMHookPoint *HP_status_base_matk_min_pre; @@ -14785,8 +14787,10 @@ struct { int HP_status_maxhp_entry_compare_post; int HP_status_read_unit_params_db_additional_pre; int HP_status_read_unit_params_db_additional_post; - int HP_status_unit_params_destroy_pre; - int HP_status_unit_params_destroy_post; + int HP_status_unit_params_destroy_entry_pre; + int HP_status_unit_params_destroy_entry_post; + int HP_status_unit_params_clear_db_pre; + int HP_status_unit_params_clear_db_post; int HP_status_copy_pre; int HP_status_copy_post; int HP_status_base_matk_min_pre; diff --git a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc index 2584bf29b05..7d3106d6eb1 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc @@ -3693,7 +3693,8 @@ struct HookingPointData HookingPoints[] = { { HP_POP(status->read_unit_params_db_maxhp, HP_status_read_unit_params_db_maxhp) }, { HP_POP(status->maxhp_entry_compare, HP_status_maxhp_entry_compare) }, { HP_POP(status->read_unit_params_db_additional, HP_status_read_unit_params_db_additional) }, - { HP_POP(status->unit_params_destroy, HP_status_unit_params_destroy) }, + { HP_POP(status->unit_params_destroy_entry, HP_status_unit_params_destroy_entry) }, + { HP_POP(status->unit_params_clear_db, HP_status_unit_params_clear_db) }, { HP_POP(status->copy, HP_status_copy) }, { HP_POP(status->base_matk_min, HP_status_base_matk_min) }, { HP_POP(status->base_matk_max, HP_status_base_matk_max) }, diff --git a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc index e6518481f51..94b60a57ff3 100644 --- a/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc +++ b/src/plugins/HPMHooking/HPMHooking_map.Hooks.inc @@ -96619,13 +96619,13 @@ bool HP_status_read_unit_params_db_additional(struct s_unit_params *entry, struc } return retVal___; } -void HP_status_unit_params_destroy(struct s_unit_params *entry) { +void HP_status_unit_params_destroy_entry(struct s_unit_params *entry) { int hIndex = 0; - if (HPMHooks.count.HP_status_unit_params_destroy_pre > 0) { + if (HPMHooks.count.HP_status_unit_params_destroy_entry_pre > 0) { void (*preHookFunc) (struct s_unit_params **entry); *HPMforce_return = false; - for (hIndex = 0; hIndex < HPMHooks.count.HP_status_unit_params_destroy_pre; hIndex++) { - preHookFunc = HPMHooks.list.HP_status_unit_params_destroy_pre[hIndex].func; + for (hIndex = 0; hIndex < HPMHooks.count.HP_status_unit_params_destroy_entry_pre; hIndex++) { + preHookFunc = HPMHooks.list.HP_status_unit_params_destroy_entry_pre[hIndex].func; preHookFunc(&entry); } if (*HPMforce_return) { @@ -96634,17 +96634,43 @@ void HP_status_unit_params_destroy(struct s_unit_params *entry) { } } { - HPMHooks.source.status.unit_params_destroy(entry); + HPMHooks.source.status.unit_params_destroy_entry(entry); } - if (HPMHooks.count.HP_status_unit_params_destroy_post > 0) { + if (HPMHooks.count.HP_status_unit_params_destroy_entry_post > 0) { void (*postHookFunc) (struct s_unit_params *entry); - for (hIndex = 0; hIndex < HPMHooks.count.HP_status_unit_params_destroy_post; hIndex++) { - postHookFunc = HPMHooks.list.HP_status_unit_params_destroy_post[hIndex].func; + for (hIndex = 0; hIndex < HPMHooks.count.HP_status_unit_params_destroy_entry_post; hIndex++) { + postHookFunc = HPMHooks.list.HP_status_unit_params_destroy_entry_post[hIndex].func; postHookFunc(entry); } } return; } +void HP_status_unit_params_clear_db(void) { + int hIndex = 0; + if (HPMHooks.count.HP_status_unit_params_clear_db_pre > 0) { + void (*preHookFunc) (void); + *HPMforce_return = false; + for (hIndex = 0; hIndex < HPMHooks.count.HP_status_unit_params_clear_db_pre; hIndex++) { + preHookFunc = HPMHooks.list.HP_status_unit_params_clear_db_pre[hIndex].func; + preHookFunc(); + } + if (*HPMforce_return) { + *HPMforce_return = false; + return; + } + } + { + HPMHooks.source.status.unit_params_clear_db(); + } + if (HPMHooks.count.HP_status_unit_params_clear_db_post > 0) { + void (*postHookFunc) (void); + for (hIndex = 0; hIndex < HPMHooks.count.HP_status_unit_params_clear_db_post; hIndex++) { + postHookFunc = HPMHooks.list.HP_status_unit_params_clear_db_post[hIndex].func; + postHookFunc(); + } + } + return; +} void HP_status_copy(struct status_data *a, const struct status_data *b) { int hIndex = 0; if (HPMHooks.count.HP_status_copy_pre > 0) {