From bab8be49af3a6bf8f3606218292602ccfe588e6e Mon Sep 17 00:00:00 2001 From: Isaac0-dev <62234577+Isaac0-dev@users.noreply.github.com> Date: Tue, 14 May 2024 09:38:53 +1000 Subject: [PATCH] fixed a crash & memory leak from smlua_text_utils (#40) * fixed a crash & memory leak from smlua_text_utils * prevent autogen from exposing smlua_text_utils_shutdown * forgot to push this --- autogen/convert_functions.py | 2 +- src/pc/crash_handler.c | 40 ++++++++-------------------- src/pc/gfx/gfx_pc.c | 1 + src/pc/lua/utils/smlua_audio_utils.c | 11 +++++--- src/pc/lua/utils/smlua_text_utils.c | 33 ++++++++++++++++------- src/pc/lua/utils/smlua_text_utils.h | 1 + src/pc/pc_main.c | 3 ++- 7 files changed, 47 insertions(+), 44 deletions(-) diff --git a/autogen/convert_functions.py b/autogen/convert_functions.py index c510b54b..213c7a7a 100644 --- a/autogen/convert_functions.py +++ b/autogen/convert_functions.py @@ -112,7 +112,7 @@ override_disallowed_functions = { "src/pc/lua/utils/smlua_audio_utils.h": [ "smlua_audio_utils_override", "audio_custom_shutdown", "smlua_audio_custom_init", "smlua_audio_custom_deinit", "audio_sample_destroy_pending_copies"], "src/pc/djui/djui_hud_utils.h": [ "djui_hud_render_texture", "djui_hud_render_texture_raw", "djui_hud_render_texture_tile", "djui_hud_render_texture_tile_raw" ], "src/pc/lua/utils/smlua_level_utils.h": [ "smlua_level_util_reset" ], - "src/pc/lua/utils/smlua_text_utils.h": [ "smlua_text_utils_init", "smlua_text_utils_reset_all" ], + "src/pc/lua/utils/smlua_text_utils.h": [ "smlua_text_utils_init", "smlua_text_utils_shutdown", "smlua_text_utils_reset_all" ], "src/pc/lua/utils/smlua_anim_utils.h": [ "smlua_anim_util_reset", "smlua_anim_util_register_animation" ], "src/pc/network/lag_compensation.h": [ "lag_compensation_clear", "lag_compensation_store" ], "src/game/first_person_cam.h": [ "first_person_update" ] diff --git a/src/pc/crash_handler.c b/src/pc/crash_handler.c index c99db192..83db21c8 100644 --- a/src/pc/crash_handler.c +++ b/src/pc/crash_handler.c @@ -28,6 +28,7 @@ char gLastRemoteBhv[256] = ""; #include "pc/mods/mods.h" #include "pc/debuglog.h" #include "pc/pc_main.h" +#include "controller/controller_keyboard.h" typedef struct { s32 x, y; @@ -201,34 +202,13 @@ void render_create_dl_ortho_matrix(void) { gSPMatrix(gDisplayListHead++, &sOrthoMatrix, G_MTX_PROJECTION | G_MTX_MUL | G_MTX_NOPUSH); } -static void crash_handler_produce_one_frame(void) { - extern u8 gRenderingInterpolated; - gRenderingInterpolated = false; - - // Start frame - gfx_start_frame(); - config_gfx_pool(); - init_render_image(); - create_dl_ortho_matrix(); - +static void crash_handler_produce_one_frame_callback(void) { float minAspectRatio = 1.743468f; float aspectScale = 1.0f; if (gfx_current_dimensions.aspect_ratio < minAspectRatio) { aspectScale = gfx_current_dimensions.aspect_ratio / minAspectRatio; } - // Fix scaling issues - extern Vp D_8032CF00; - gSPViewport(gDisplayListHead++, VIRTUAL_TO_PHYSICAL(&D_8032CF00)); - gDPSetScissor(gDisplayListHead++, G_SC_NON_INTERLACE, 0, BORDER_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - BORDER_HEIGHT); - - // Clear screen - create_dl_translation_matrix(MENU_MTX_PUSH, GFX_DIMENSIONS_FROM_LEFT_EDGE(0), 240.f, 0.f); - create_dl_scale_matrix(MENU_MTX_NOPUSH, (GFX_DIMENSIONS_ASPECT_RATIO * SCREEN_HEIGHT) / 130.f, 3.f, 1.f); - gDPSetEnvColor(gDisplayListHead++, 0x02, 0x06, 0x0F, 0xFF); - gSPDisplayList(gDisplayListHead++, dl_draw_text_bg_box); - gSPPopMatrix(gDisplayListHead++, G_MTX_MODELVIEW); - // Print text const struct DjuiFont* font = gDjuiFonts[0]; if (font->textBeginDisplayList != NULL) { @@ -274,13 +254,12 @@ static void crash_handler_produce_one_frame(void) { // pop gSPPopMatrix(gDisplayListHead++, G_MTX_MODELVIEW); } +} - // Render frame - end_master_display_list(); - alloc_display_list(0); - gfx_run((Gfx*) gGfxSPTask->task.t.data_ptr); // send_display_list - display_and_vsync(); - gfx_end_frame(); +static void crash_handler_produce_one_frame(void) { + extern u8 gRenderingInterpolated; + gRenderingInterpolated = false; + produce_one_dummy_frame(crash_handler_produce_one_frame_callback); } static void crash_handler_add_info_str(CrashHandlerText** pTextP, f32 x, f32 y, const char* title, const char* value) { @@ -675,7 +654,10 @@ static void crash_handler(const int signalNum, siginfo_t *info, UNUSED ucontext_ #endif // In case the game crashed before the game window opened - if (!gGfxInited) gfx_init(&WAPI, &RAPI, TITLE); + if (!gGfxInited) { + gfx_init(&WAPI, &RAPI, TITLE); + WAPI.set_keyboard_callbacks(keyboard_on_key_down, keyboard_on_key_up, keyboard_on_all_keys_up, keyboard_on_text_input); + } if (!gGameInited) djui_unicode_init(); // Main loop diff --git a/src/pc/gfx/gfx_pc.c b/src/pc/gfx/gfx_pc.c index 7f1dbd97..eb0e72c7 100644 --- a/src/pc/gfx/gfx_pc.c +++ b/src/pc/gfx/gfx_pc.c @@ -1865,6 +1865,7 @@ void gfx_shutdown(void) { if (gfx_wapi->shutdown) gfx_wapi->shutdown(); gfx_wapi = NULL; } + gGfxInited = false; } ///////////////////////// diff --git a/src/pc/lua/utils/smlua_audio_utils.c b/src/pc/lua/utils/smlua_audio_utils.c index 0bd0e70a..0a042f20 100644 --- a/src/pc/lua/utils/smlua_audio_utils.c +++ b/src/pc/lua/utils/smlua_audio_utils.c @@ -159,8 +159,8 @@ void smlua_audio_utils_replace_sequence(u8 sequenceId, u8 bankId, u8 defaultVolu #define MA_SOUND_STREAM_FLAGS (MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_STREAM) #define MA_SOUND_SAMPLE_FLAGS (MA_SOUND_FLAG_NO_SPATIALIZATION | MA_SOUND_FLAG_NO_PITCH | MA_SOUND_FLAG_DECODE) // No pitch, pre-decode audio samples -struct DynamicPool *sModAudio; ma_engine gModAudioEngine; +static struct DynamicPool *sModAudio; static struct ModAudio* find_mod_audio(struct ModFile* file) { struct DynamicPoolNode* node = sModAudio->tail; @@ -543,7 +543,10 @@ void smlua_audio_custom_init(void) { } void smlua_audio_custom_deinit(void) { - audio_custom_shutdown(); - free(sModAudio); - ma_engine_uninit(&gModAudioEngine); + if (sModAudio) { + audio_custom_shutdown(); + free(sModAudio); + ma_engine_uninit(&gModAudioEngine); + sModAudio = NULL; + } } diff --git a/src/pc/lua/utils/smlua_text_utils.c b/src/pc/lua/utils/smlua_text_utils.c index e0ec18b5..f3a1a978 100644 --- a/src/pc/lua/utils/smlua_text_utils.c +++ b/src/pc/lua/utils/smlua_text_utils.c @@ -26,6 +26,8 @@ void convert_string_sm64_to_ascii(char *strAscii, const u8 *str64); struct CourseName *gReplacedActNameTable[COURSE_END]; +static bool sSmluaTextUtilsInited = false; + // Save all vanilla act names and course names void smlua_text_utils_init(void) { void **actNameTbl = get_act_name_table(); @@ -55,6 +57,17 @@ void smlua_text_utils_init(void) { courseActNames->actName[MAX_ACTS_AND_100_COINS - 1].modIndex = -1; } } + sSmluaTextUtilsInited = true; +} + +void smlua_text_utils_shutdown(void) { + if (sSmluaTextUtilsInited) { + for (s16 courseNum = 0; courseNum < COURSE_END; courseNum++) { + free(gReplacedActNameTable[courseNum]->actName); + free(gReplacedActNameTable[courseNum]); + } + sSmluaTextUtilsInited = false; + } } static u8* smlua_text_utils_convert(const char* str) { @@ -130,16 +143,18 @@ void smlua_text_utils_reset_all(void) { sReplacedActName[i] = false; } - for (s32 courseNum = 0; courseNum < COURSE_COUNT; courseNum++) { - struct CourseName* courseActNames = gReplacedActNameTable[courseNum]; - snprintf(courseActNames->name, 50, "%s", courseActNames->orig); - courseActNames->modIndex = -1; + if (sSmluaTextUtilsInited) { + for (s32 courseNum = 0; courseNum < COURSE_COUNT; courseNum++) { + struct CourseName* courseActNames = gReplacedActNameTable[courseNum]; + snprintf(courseActNames->name, 50, "%s", courseActNames->orig); + courseActNames->modIndex = -1; - // Individual acts - if (COURSE_IS_MAIN_COURSE(courseNum)) { - for (s16 actNum = 0; actNum < MAX_ACTS_AND_100_COINS; actNum++) { - snprintf(courseActNames->actName[actNum].name, 50, "%s", courseActNames->actName[actNum].orig); - courseActNames->actName[actNum].modIndex = -1; + // Individual acts + if (COURSE_IS_MAIN_COURSE(courseNum)) { + for (s16 actNum = 0; actNum < MAX_ACTS_AND_100_COINS; actNum++) { + snprintf(courseActNames->actName[actNum].name, 50, "%s", courseActNames->actName[actNum].orig); + courseActNames->actName[actNum].modIndex = -1; + } } } } diff --git a/src/pc/lua/utils/smlua_text_utils.h b/src/pc/lua/utils/smlua_text_utils.h index 6d76cad2..4b325c0b 100644 --- a/src/pc/lua/utils/smlua_text_utils.h +++ b/src/pc/lua/utils/smlua_text_utils.h @@ -23,6 +23,7 @@ struct CourseName { extern struct CourseName *gReplacedActNameTable[]; void smlua_text_utils_init(void); +void smlua_text_utils_shutdown(void); void smlua_text_utils_reset_all(void); void smlua_text_utils_dialog_replace(enum DialogId dialogId, u32 unused, s8 linesPerBox, s16 leftOffset, s16 width, const char* str); void smlua_text_utils_course_acts_replace(s16 courseNum, const char* courseName, const char* act1, const char* act2, const char* act3, const char* act4, const char* act5, const char* act6); diff --git a/src/pc/pc_main.c b/src/pc/pc_main.c index 21dd0bbf..9c1d9ff4 100644 --- a/src/pc/pc_main.c +++ b/src/pc/pc_main.c @@ -291,12 +291,13 @@ void game_deinit(void) { controller_shutdown(); audio_custom_shutdown(); audio_shutdown(); - gfx_shutdown(); network_shutdown(true, true, false, false); + smlua_text_utils_shutdown(); smlua_shutdown(); smlua_audio_custom_deinit(); mods_shutdown(); djui_shutdown(); + gfx_shutdown(); gGameInited = false; }