From 4ea2fc4749d3decbf75d5f859b911e175fd1c04f Mon Sep 17 00:00:00 2001 From: BitPatty Date: Tue, 31 Jan 2017 01:18:03 +0100 Subject: [PATCH] Fixes memory leak caused by J2DTextBox_SetString() Setting the string on each frame fills up unallocated memory and causes a hardlock. Instead, call J2DTextBox_SetString() on stage load (since the memory is freed level transition) and modify the already set string directly on each frame. --- sms.c | 8 +++++--- sms2.c | 11 ++++++----- 2 files changed, 11 insertions(+), 8 deletions(-) diff --git a/sms.c b/sms.c index 09f173f..2a11593 100644 --- a/sms.c +++ b/sms.c @@ -6,7 +6,7 @@ #include "sms.h" static J2DTextBox textbox; -static char* info; + int OnUpdate(MarDirector* director) { int (*GameUpdate)(MarDirector* director) = GetObjectFunction(director, Director_GameUpdate); @@ -19,9 +19,8 @@ void OnDraw2D(J2DOrthoGraph* graphics) { Vector mariopos = **gpMarioPos; int state = GetMarioStatus(); - snprintf(info, 128, "Mario X: %f\nMario Y: %f\nMario Z: %f\nMario State: %X\nNext Stage: %d-%d", + snprintf(J2DTextBox_GetStringPtr(&textbox), 128, "Mario X: %f\nMario Y: %f\nMario Z: %f\nMario State: %X\nNext Stage: %d-%d", mariopos.x, mariopos.y, mariopos.z, state, *ChangeScenario, *ChangeEpisode); - J2DTextBox_SetString(&textbox, info); J2DGrafContext_Setup2D((J2DGrafContext*)graphics); //Run replaced branch @@ -39,5 +38,8 @@ void OnSetup(MarDirector* director) //textbox = (J2DTextBox*)malloc(sizeof(J2DTextBox)); J2DTextBox_Create(&textbox, 0, &rect, GameFont, GameStrTable, 2, 0); + char* info; info = (char*)malloc(128); + + J2DTextBox_SetString(&textbox, info); } \ No newline at end of file diff --git a/sms2.c b/sms2.c index 768c2f3..ccd4fc4 100644 --- a/sms2.c +++ b/sms2.c @@ -13,7 +13,7 @@ /* This code is always running, and is constantly updating. */ J2DTextBox textbox; -char* info; + bool inAir; int timesjumped; @@ -59,10 +59,7 @@ void OnDraw2D(J2DOrthoGraph* graphics) /* Here are his states that we read. If Mario is doing said action, the return should be 1. */ - snprintf(info, 128, "Time x %d", time); - - // now we set our string to a J2DTextBox - J2DTextBox_SetString(&textbox, info); + snprintf(J2DTextBox_GetStringPtr(&textbox), 128, "Time x %d", time); //Run replaced branch J2DGrafContext_Setup2D((J2DGrafContext*)graphics); @@ -93,6 +90,10 @@ void OnSetup(MarDirector* director) //textbox = (J2DTextBox*)malloc(sizeof(J2DTextBox)); J2DTextBox_Create(&textbox, 0, &rect, GameFont, GameStrTable, 2, 0); + char* info; + info = (char*)malloc(128); + J2DTextBox_SetString(&textbox, info); + void (*TestNull)(void) = 0x80247fa4; TestNull();