Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix memory leak when reloading unit params db #3288

Merged
merged 1 commit into from
Apr 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 22 additions & 11 deletions src/map/status.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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;
}

Expand All @@ -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));

Expand Down Expand Up @@ -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);

Expand All @@ -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
Expand All @@ -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++ )
Expand Down Expand Up @@ -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");
Expand All @@ -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();
}

/*=====================================
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/map/status.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
6 changes: 4 additions & 2 deletions src/plugins/HPMHooking/HPMHooking.Defs.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
12 changes: 8 additions & 4 deletions src/plugins/HPMHooking/HPMHooking_map.HPMHooksCore.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/HPMHooking/HPMHooking_map.HookingPoints.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) },
Expand Down
42 changes: 34 additions & 8 deletions src/plugins/HPMHooking/HPMHooking_map.Hooks.inc
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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) {
Expand Down
Loading