Skip to content

Commit 9dd68cd

Browse files
committed
Fixed a memory leak with pouchInit being called more than once
This is mainly for the warping by events in the Warps menu, as one of the init functions calls pouchInit.
1 parent ed53efb commit 9dd68cd

File tree

1 file changed

+22
-1
lines changed

1 file changed

+22
-1
lines changed

ttyd-tools/rel/source/main.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,10 +18,10 @@
1818
#include <ttyd/mario.h>
1919
#include <ttyd/mario_party.h>
2020
#include <ttyd/party.h>
21+
#include <ttyd/memory.h>
2122
#include <ttyd/seq_title.h>
2223
#include <ttyd/mario_pouch.h>
2324
#include <ttyd/sac_scissor.h>
24-
#include <ttyd/memory.h>
2525

2626
#include <cstdio>
2727
#include <cinttypes>
@@ -215,6 +215,22 @@ void *fixEvtMapBlendSetFlagFollowerCrash(void *followerPtr)
215215
}
216216
}
217217

218+
void *preventPouchInitMemoryLeak(uint32_t heap, uint32_t size)
219+
{
220+
// Check if the memory has already been allocated or not
221+
void *PouchPtr = ttyd::mario_pouch::pouchGetPtr();
222+
if (!PouchPtr)
223+
{
224+
// The memory has not been allocated, so allocate it
225+
return ttyd::memory::__memAlloc(heap, size);
226+
}
227+
else
228+
{
229+
// The memory has already been allocated, so clear it
230+
return clearMemory(PouchPtr, size);
231+
}
232+
}
233+
218234
const char *replaceJumpFallAnim(char *jumpFallString)
219235
{
220236
if (compareStringsSize(jumpFallString, "M_J_", 4))
@@ -609,6 +625,7 @@ void initAddressOverwrites()
609625
void *DisableDPadOptionsDisplayAddress = reinterpret_cast<void *>(0x8013D148);
610626
void *FixEvtMapBlendSetFlagPartnerCrashAddress = reinterpret_cast<void *>(0x800389C4);
611627
void *FixEvtMapBlendSetFlagFollowerCrashAddress = reinterpret_cast<void *>(0x80038A0C);
628+
void *PreventPouchInitMemoryLeakAddress = reinterpret_cast<void *>(0x800D59DC);
612629
void *InitStageEventsAddress = reinterpret_cast<void *>(0x800080E4);
613630
void *FallThroughMostObjectsStandAddress = reinterpret_cast<void *>(0x8008E9DC);
614631
void *FallThroughMostObjectsTubeAddress = reinterpret_cast<void *>(0x8008E1E8);
@@ -633,6 +650,7 @@ void initAddressOverwrites()
633650
void *DisableDPadOptionsDisplayAddress = reinterpret_cast<void *>(0x80137C1C);
634651
void *FixEvtMapBlendSetFlagPartnerCrashAddress = reinterpret_cast<void *>(0x80038328);
635652
void *FixEvtMapBlendSetFlagFollowerCrashAddress = reinterpret_cast<void *>(0x80038370);
653+
void *PreventPouchInitMemoryLeakAddress = reinterpret_cast<void *>(0x800D17A8);
636654
void *InitStageEventsAddress = reinterpret_cast<void *>(0x80008054);
637655
void *FallThroughMostObjectsStandAddress = reinterpret_cast<void *>(0x8008D428);
638656
void *FallThroughMostObjectsTubeAddress = reinterpret_cast<void *>(0x8008CC4C);
@@ -659,6 +677,7 @@ void initAddressOverwrites()
659677
void *DisableDPadOptionsDisplayAddress = reinterpret_cast<void *>(0x8013EC30);
660678
void *FixEvtMapBlendSetFlagPartnerCrashAddress = reinterpret_cast<void *>(0x80038AAC);
661679
void *FixEvtMapBlendSetFlagFollowerCrashAddress = reinterpret_cast<void *>(0x80038AF4);
680+
void *PreventPouchInitMemoryLeakAddress = reinterpret_cast<void *>(0x800D67E4);
662681
void *InitStageEventsAddress = reinterpret_cast<void *>(0x800082BC);
663682
void *FallThroughMostObjectsStandAddress = reinterpret_cast<void *>(0x8008FD38);
664683
void *FallThroughMostObjectsTubeAddress = reinterpret_cast<void *>(0x8008F544);
@@ -689,6 +708,8 @@ void initAddressOverwrites()
689708

690709
patch::writeBranchLR(FixEvtMapBlendSetFlagFollowerCrashAddress, reinterpret_cast<void *>(StartFixEvtMapBlendSetFlagFollowerCrash));
691710

711+
patch::writeBranchLR(PreventPouchInitMemoryLeakAddress, reinterpret_cast<void *>(preventPouchInitMemoryLeak));
712+
692713
patch::writeBranchLR(InitStageEventsAddress, reinterpret_cast<void *>(initStageEvents));
693714

694715
patch::writeBranchLR(FallThroughMostObjectsStandAddress, reinterpret_cast<void *>(StartFallThroughMostObjectsStandard));

0 commit comments

Comments
 (0)