Fix memory leak in OTRMessage_Init (#466)

This commit is contained in:
Garrett Cox 2024-05-24 12:34:35 -05:00 committed by GitHub
parent 86b5f10b26
commit f270156409
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 14 additions and 26 deletions

View File

@ -488,7 +488,7 @@ extern "C" void InitOTR() {
DebugConsole_Init();
clearMtx = (uintptr_t)&gMtxClear;
// OTRMessage_Init();
OTRMessage_Init();
OTRAudio_Init();
// OTRExtScanner();

View File

@ -6,8 +6,8 @@
#include "2s2h/resource/type/TextMM.h"
#include <message_data_static.h>
// extern "C" MessageTableEntry* sNesMessageEntryTablePtr;
// extern "C" MessageTableEntry* sStaffMessageEntryTablePtr;
extern "C" MessageTableEntry* sMessageTableNES;
extern "C" MessageTableEntry* sMessageTableCredits;
MessageTableEntry* OTRMessage_LoadTable(const char* filePath, bool isNES) {
auto file = std::static_pointer_cast<SOH::TextMM>(
@ -51,27 +51,17 @@ MessageTableEntry* OTRMessage_LoadTable(const char* filePath, bool isNES) {
return table;
}
extern "C" void OTRMessage_Init(PlayState* play) {
// OTRTODO: Added a lot of null checks here so that we don't malloc the table multiple times causing a memory leak.
// We really ought to fix the implementation such that we aren't malloc'ing new tables.
// Once we fix the implementation, remove these NULL checks.
// if (play->msgCtx.messageEntryTableNes == NULL) {
play->msgCtx.messageEntryTableNes = OTRMessage_LoadTable("text/message_data_static/message_data_static", true);
play->msgCtx.messageEntryTable = play->msgCtx.messageEntryTableNes;
//}
extern "C" void OTRMessage_Init() {
sMessageTableNES = OTRMessage_LoadTable("text/message_data_static/message_data_static", true);
// if (play->msgCtx.messageTableStaff == NULL) {
auto file2 = std::static_pointer_cast<SOH::TextMM>(Ship::Context::GetInstance()->GetResourceManager()->LoadResource(
"text/staff_message_data_static/staff_message_data_static"));
// OTRTODO: Should not be malloc'ing here. It's fine for now since we check that the message table is already null.
play->msgCtx.messageTableStaff = (MessageTableEntry*)malloc(sizeof(MessageTableEntry) * file2->messages.size());
sMessageTableCredits = (MessageTableEntry*)malloc(sizeof(MessageTableEntry) * file2->messages.size());
for (size_t i = 0; i < file2->messages.size(); i++) {
play->msgCtx.messageTableStaff[i].textId = file2->messages[i].id;
play->msgCtx.messageTableStaff[i].typePos =
(file2->messages[i].textboxType << 4) | file2->messages[i].textboxYPos;
play->msgCtx.messageTableStaff[i].segment = file2->messages[i].msg.c_str();
play->msgCtx.messageTableStaff[i].msgSize = file2->messages[i].msg.size();
sMessageTableCredits[i].textId = file2->messages[i].id;
sMessageTableCredits[i].typePos = (file2->messages[i].textboxType << 4) | file2->messages[i].textboxYPos;
sMessageTableCredits[i].segment = file2->messages[i].msg.c_str();
sMessageTableCredits[i].msgSize = file2->messages[i].msg.size();
}
//}
}

View File

@ -205,9 +205,8 @@ u16 gBombersNotebookWeekEventFlags[BOMBERS_NOTEBOOK_EVENT_MAX] = {
#undef DEFINE_PERSON
#undef DEFINE_EVENT
// TODO: Scripts
// Include message tables D_801C6B98 and D_801CFB08
#include "src/code/z_message_tables.inc"
MessageTableEntry* sMessageTableNES;
MessageTableEntry* sMessageTableCredits;
s16 D_801CFC78[TEXTBOX_TYPE_MAX] = {
0, // TEXTBOX_TYPE_0
@ -6216,9 +6215,8 @@ void Message_Update(PlayState* play) {
}
void Message_SetTables(PlayState* play) {
// play->msgCtx.messageEntryTableNes = D_801C6B98;
// play->msgCtx.messageTableStaff = D_801CFB08;
OTRMessage_Init(play);
play->msgCtx.messageEntryTableNes = sMessageTableNES;
play->msgCtx.messageTableStaff = sMessageTableCredits;
}
void Message_Init(PlayState* play) {