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
This commit is contained in:
Isaac0-dev 2024-05-14 09:38:53 +10:00 committed by GitHub
parent 45d42dbf73
commit bab8be49af
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 47 additions and 44 deletions

View File

@ -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/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/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_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/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/pc/network/lag_compensation.h": [ "lag_compensation_clear", "lag_compensation_store" ],
"src/game/first_person_cam.h": [ "first_person_update" ] "src/game/first_person_cam.h": [ "first_person_update" ]

View File

@ -28,6 +28,7 @@ char gLastRemoteBhv[256] = "";
#include "pc/mods/mods.h" #include "pc/mods/mods.h"
#include "pc/debuglog.h" #include "pc/debuglog.h"
#include "pc/pc_main.h" #include "pc/pc_main.h"
#include "controller/controller_keyboard.h"
typedef struct { typedef struct {
s32 x, y; 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); gSPMatrix(gDisplayListHead++, &sOrthoMatrix, G_MTX_PROJECTION | G_MTX_MUL | G_MTX_NOPUSH);
} }
static void crash_handler_produce_one_frame(void) { static void crash_handler_produce_one_frame_callback(void) {
extern u8 gRenderingInterpolated;
gRenderingInterpolated = false;
// Start frame
gfx_start_frame();
config_gfx_pool();
init_render_image();
create_dl_ortho_matrix();
float minAspectRatio = 1.743468f; float minAspectRatio = 1.743468f;
float aspectScale = 1.0f; float aspectScale = 1.0f;
if (gfx_current_dimensions.aspect_ratio < minAspectRatio) { if (gfx_current_dimensions.aspect_ratio < minAspectRatio) {
aspectScale = 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 // Print text
const struct DjuiFont* font = gDjuiFonts[0]; const struct DjuiFont* font = gDjuiFonts[0];
if (font->textBeginDisplayList != NULL) { if (font->textBeginDisplayList != NULL) {
@ -274,13 +254,12 @@ static void crash_handler_produce_one_frame(void) {
// pop // pop
gSPPopMatrix(gDisplayListHead++, G_MTX_MODELVIEW); gSPPopMatrix(gDisplayListHead++, G_MTX_MODELVIEW);
} }
}
// Render frame static void crash_handler_produce_one_frame(void) {
end_master_display_list(); extern u8 gRenderingInterpolated;
alloc_display_list(0); gRenderingInterpolated = false;
gfx_run((Gfx*) gGfxSPTask->task.t.data_ptr); // send_display_list produce_one_dummy_frame(crash_handler_produce_one_frame_callback);
display_and_vsync();
gfx_end_frame();
} }
static void crash_handler_add_info_str(CrashHandlerText** pTextP, f32 x, f32 y, const char* title, const char* value) { 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 #endif
// In case the game crashed before the game window opened // 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(); if (!gGameInited) djui_unicode_init();
// Main loop // Main loop

View File

@ -1865,6 +1865,7 @@ void gfx_shutdown(void) {
if (gfx_wapi->shutdown) gfx_wapi->shutdown(); if (gfx_wapi->shutdown) gfx_wapi->shutdown();
gfx_wapi = NULL; gfx_wapi = NULL;
} }
gGfxInited = false;
} }
///////////////////////// /////////////////////////

View File

@ -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_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 #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; ma_engine gModAudioEngine;
static struct DynamicPool *sModAudio;
static struct ModAudio* find_mod_audio(struct ModFile* file) { static struct ModAudio* find_mod_audio(struct ModFile* file) {
struct DynamicPoolNode* node = sModAudio->tail; struct DynamicPoolNode* node = sModAudio->tail;
@ -543,7 +543,10 @@ void smlua_audio_custom_init(void) {
} }
void smlua_audio_custom_deinit(void) { void smlua_audio_custom_deinit(void) {
audio_custom_shutdown(); if (sModAudio) {
free(sModAudio); audio_custom_shutdown();
ma_engine_uninit(&gModAudioEngine); free(sModAudio);
ma_engine_uninit(&gModAudioEngine);
sModAudio = NULL;
}
} }

View File

@ -26,6 +26,8 @@ void convert_string_sm64_to_ascii(char *strAscii, const u8 *str64);
struct CourseName *gReplacedActNameTable[COURSE_END]; struct CourseName *gReplacedActNameTable[COURSE_END];
static bool sSmluaTextUtilsInited = false;
// Save all vanilla act names and course names // Save all vanilla act names and course names
void smlua_text_utils_init(void) { void smlua_text_utils_init(void) {
void **actNameTbl = get_act_name_table(); 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; 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) { static u8* smlua_text_utils_convert(const char* str) {
@ -130,16 +143,18 @@ void smlua_text_utils_reset_all(void) {
sReplacedActName[i] = false; sReplacedActName[i] = false;
} }
for (s32 courseNum = 0; courseNum < COURSE_COUNT; courseNum++) { if (sSmluaTextUtilsInited) {
struct CourseName* courseActNames = gReplacedActNameTable[courseNum]; for (s32 courseNum = 0; courseNum < COURSE_COUNT; courseNum++) {
snprintf(courseActNames->name, 50, "%s", courseActNames->orig); struct CourseName* courseActNames = gReplacedActNameTable[courseNum];
courseActNames->modIndex = -1; snprintf(courseActNames->name, 50, "%s", courseActNames->orig);
courseActNames->modIndex = -1;
// Individual acts // Individual acts
if (COURSE_IS_MAIN_COURSE(courseNum)) { if (COURSE_IS_MAIN_COURSE(courseNum)) {
for (s16 actNum = 0; actNum < MAX_ACTS_AND_100_COINS; actNum++) { for (s16 actNum = 0; actNum < MAX_ACTS_AND_100_COINS; actNum++) {
snprintf(courseActNames->actName[actNum].name, 50, "%s", courseActNames->actName[actNum].orig); snprintf(courseActNames->actName[actNum].name, 50, "%s", courseActNames->actName[actNum].orig);
courseActNames->actName[actNum].modIndex = -1; courseActNames->actName[actNum].modIndex = -1;
}
} }
} }
} }

View File

@ -23,6 +23,7 @@ struct CourseName {
extern struct CourseName *gReplacedActNameTable[]; extern struct CourseName *gReplacedActNameTable[];
void smlua_text_utils_init(void); void smlua_text_utils_init(void);
void smlua_text_utils_shutdown(void);
void smlua_text_utils_reset_all(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_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); 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);

View File

@ -291,12 +291,13 @@ void game_deinit(void) {
controller_shutdown(); controller_shutdown();
audio_custom_shutdown(); audio_custom_shutdown();
audio_shutdown(); audio_shutdown();
gfx_shutdown();
network_shutdown(true, true, false, false); network_shutdown(true, true, false, false);
smlua_text_utils_shutdown();
smlua_shutdown(); smlua_shutdown();
smlua_audio_custom_deinit(); smlua_audio_custom_deinit();
mods_shutdown(); mods_shutdown();
djui_shutdown(); djui_shutdown();
gfx_shutdown();
gGameInited = false; gGameInited = false;
} }