From 3f9873a754babf86c5cba2a205af8fbfe9073f2a Mon Sep 17 00:00:00 2001 From: Bill Currie Date: Sun, 5 Mar 2023 17:10:04 +0900 Subject: [PATCH] [console] Plug more memory leaks --- include/QF/console.h | 1 + libs/console/bi_inputline.c | 4 ++- libs/console/client.c | 53 +++++++++++++++++++++++--------- libs/console/console.c | 1 - libs/console/menu.c | 11 +++++-- ruamoko/qwaq/builtins/graphics.c | 1 + 6 files changed, 53 insertions(+), 18 deletions(-) diff --git a/include/QF/console.h b/include/QF/console.h index 0a0274664..161200fdf 100644 --- a/include/QF/console.h +++ b/include/QF/console.h @@ -110,6 +110,7 @@ void C_DrawInputLine (struct inputline_s *il); struct view_s; void Menu_Init (void); +void Menu_Shutdown (void); void Menu_Load (void); void Menu_Draw (struct view_s view); void Menu_Draw_Hud (struct view_s view); diff --git a/libs/console/bi_inputline.c b/libs/console/bi_inputline.c index 4b6764e3a..78cf923f3 100644 --- a/libs/console/bi_inputline.c +++ b/libs/console/bi_inputline.c @@ -105,7 +105,9 @@ bi_il_clear (progs_t *pr, void *_res) static void bi_il_destroy (progs_t *pr, void *_res) { - free (_res); + il_resources_t *res = _res; + PR_RESDELMAP (res->line_map); + free (res); } static il_data_t * __attribute__((pure)) diff --git a/libs/console/client.c b/libs/console/client.c index b2268be98..b274883ab 100644 --- a/libs/console/client.c +++ b/libs/console/client.c @@ -160,6 +160,10 @@ static int con_saved_focos; static qboolean con_debuglog; static qboolean chat_team; +static dstring_t *c_print_buffer; +static dstring_t *dlbar; +static dstring_t *old_gamedir = 0; + typedef struct { const char *prompt; @@ -448,26 +452,25 @@ static __attribute__((format(PRINTF, 1, 0))) void C_Print (const char *fmt, va_list args) { char *s; - static dstring_t *buffer; int mask, c; - if (!buffer) - buffer = dstring_new (); + if (!c_print_buffer) + c_print_buffer = dstring_new (); - dvsprintf (buffer, fmt, args); + dvsprintf (c_print_buffer, fmt, args); // log all messages to file if (con_debuglog) Sys_DebugLog (va (0, "%s/%s/qconsole.log", qfs_userpath,//FIXME - qfs_gamedir->dir.def), "%s", buffer->str); + qfs_gamedir->dir.def), "%s", c_print_buffer->str); if (!con_initialized) return; - s = buffer->str; + s = c_print_buffer->str; mask = 0; - if (buffer->str[0] == 1 || buffer->str[0] == 2) { + if (s[0] == 1 || s[0] == 2) { mask = 128; // go to colored text s++; } @@ -506,10 +509,10 @@ C_Print (const char *fmt, va_list args) // echo to debugging console // but don't print the highchars flag (leading \x01) - if ((byte)buffer->str[0] > 2) - fputs (buffer->str, stdout); - else if ((byte)buffer->str[0]) - fputs (buffer->str + 1, stdout); + if ((byte)c_print_buffer->str[0] > 2) + fputs (c_print_buffer->str, stdout); + else if ((byte)c_print_buffer->str[0]) + fputs (c_print_buffer->str + 1, stdout); } /* DRAWING */ @@ -582,7 +585,6 @@ resize_input (view_t view, view_pos_t len) static void update_download (void) { - static dstring_t *dlbar; const char *text; if (!dlbar) { @@ -815,8 +817,6 @@ C_DrawConsole (void) static void C_NewMap (void) { - static dstring_t *old_gamedir = 0; - if (!old_gamedir || !strequal (old_gamedir->str, qfs_gamedir->gamedir)) Menu_Load (); if (!old_gamedir) @@ -1193,7 +1193,32 @@ C_Init (void) static void C_shutdown (void) { + r_funcs->Draw_DestroyPic (conback); IE_Remove_Handler (con_event_id); + Menu_Shutdown (); + + Con_DestroyInputLine (cmd_line.input_line); + Con_DestroyInputLine (team_input); + Con_DestroyInputLine (chat_input); + Con_DestroyBuffer (con_main); + + if (download_buffer) { + Draw_DestroyBuffer (download_buffer); + } + Draw_DestroyBuffer (console_buffer); + Draw_DestroyBuffer (cmd_line.buffer); + Draw_DestroyBuffer (notify_buffer); + Draw_DestroyBuffer (say_line.buffer); + + if (c_print_buffer) { + dstring_delete (c_print_buffer); + } + if (dlbar) { + dstring_delete (dlbar); + } + if (old_gamedir) { + dstring_delete (old_gamedir); + } } static general_data_t plugin_info_general_data; diff --git a/libs/console/console.c b/libs/console/console.c index 43964db03..9c3fc8140 100644 --- a/libs/console/console.c +++ b/libs/console/console.c @@ -102,7 +102,6 @@ Con_shutdown (void *data) Sys_SetStdPrintf (saved_sys_printf); } if (con_module) { - con_module->functions->general->shutdown (); PI_UnloadPlugin (con_module); } } diff --git a/libs/console/menu.c b/libs/console/menu.c index c1c085a03..af64b90ba 100644 --- a/libs/console/menu.c +++ b/libs/console/menu.c @@ -99,7 +99,7 @@ static pr_func_t menu_quit; static pr_func_t menu_draw_hud; static pr_func_t menu_pre; static pr_func_t menu_post; -static const char *top_menu; +static char *top_menu; typedef struct menu_func_s { const char *name; @@ -365,7 +365,7 @@ bi_Menu_TopMenu (progs_t *pr, void *data) const char *name = P_GSTRING (pr, 0); if (top_menu) - free ((char *) top_menu); + free (top_menu); top_menu = strdup (name); } @@ -591,6 +591,13 @@ Menu_Next_f (void) Menu_KeyEvent (QFK_DOWN, '\0', true); } +void +Menu_Shutdown (void) +{ + PR_Shutdown (&menu_pr_state); + Hash_DelTable (menu_hash); + free (top_menu); +} void Menu_Init (void) diff --git a/ruamoko/qwaq/builtins/graphics.c b/ruamoko/qwaq/builtins/graphics.c index 49b87abe8..04f477064 100644 --- a/ruamoko/qwaq/builtins/graphics.c +++ b/ruamoko/qwaq/builtins/graphics.c @@ -161,6 +161,7 @@ event_handler (const IE_event_t *ie_event, void *_pr) static void BI_shutdown (void *data) { + ECS_DelRegistry (canvas_sys.reg); } static byte default_palette[256][3];