Skip to content

Commit 64d962f

Browse files
committed
I believe I fixed the memory leaks from SDL_Texture
1 parent 6a00c1f commit 64d962f

File tree

1 file changed

+53
-6
lines changed

1 file changed

+53
-6
lines changed

src/meowstro.cpp

Lines changed: 53 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
// Names: Hugo, Jaime, Jay, Leo
2-
// Last Modified: 05/17/25
2+
// Last Modified: 08/30/25
33
// Purpose: MEOWSTRO
44
//
55
//
@@ -68,7 +68,7 @@ int main(int argc, char** argv)
6868
stats.resetStats();
6969
}
7070
}
71-
window.~RenderWindow();
71+
// Destructor will be called automatically when window goes out of scope
7272

7373
TTF_Quit();
7474
SDL_Quit();
@@ -144,6 +144,13 @@ void mainMenu(RenderWindow &window, bool &gameRunning, SDL_Event &event)
144144
window.render(quit);
145145
window.display();
146146
}
147+
148+
SDL_DestroyTexture(quitTexture);
149+
SDL_DestroyTexture(startTexture);
150+
SDL_DestroyTexture(logoTexture);
151+
SDL_DestroyTexture(logoCatTexture);
152+
SDL_DestroyTexture(selectedTexture);
153+
147154
logoFont.unload();
148155
startFont.unload();
149156
quitFont.unload();
@@ -238,9 +245,18 @@ void gameLoop(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameSta
238245

239246
double currentTime = SDL_GetTicks() - songStartTime; //calculates the delay by comparing the current ticks and when the song starts
240247

241-
std::string strNum = formatScore(stats.getScore());
242-
numberTexture = numberFont.renderText(window.getRenderer(), strNum, { 0, 0, 0, 255 });
243-
number.setTexture(numberTexture);
248+
// Only update score texture when score actually changes to prevent memory leaks
249+
static int lastScore = -1;
250+
int currentScore = stats.getScore();
251+
if (currentScore != lastScore) {
252+
if (lastScore != -1) {
253+
SDL_DestroyTexture(numberTexture); // Clean up old texture
254+
}
255+
std::string strNum = formatScore(currentScore);
256+
numberTexture = numberFont.renderText(window.getRenderer(), strNum, { 0, 0, 0, 255 });
257+
number.setTexture(numberTexture);
258+
lastScore = currentScore;
259+
}
244260
handX = fisher.getX() + 135;
245261
handY = fisher.getY() + 50;
246262

@@ -428,9 +444,24 @@ void gameLoop(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameSta
428444
SDL_Delay(75);
429445
}
430446
player.stopBackgroundMusic();
431-
player.~Audio();
447+
448+
// Clean up game loop textures to prevent memory leaks
449+
for (int i = 0; i < NUM_FISH_TEXTURES; i++) {
450+
SDL_DestroyTexture(fishTextures[i]);
451+
}
452+
SDL_DestroyTexture(oceanTexture);
453+
SDL_DestroyTexture(boatTexture);
454+
SDL_DestroyTexture(fisherTexture);
455+
SDL_DestroyTexture(hookTexture);
456+
SDL_DestroyTexture(scoreTexture);
457+
SDL_DestroyTexture(numberTexture);
458+
SDL_DestroyTexture(perfectHitTexture);
459+
SDL_DestroyTexture(goodHitTexture);
460+
432461
scoreFont.unload();
433462
numberFont.unload();
463+
perfectHitFont.unload();
464+
goodHitFont.unload();
434465
}
435466

436467
void endScreen(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameStats& stats)
@@ -539,6 +570,22 @@ void endScreen(RenderWindow& window, bool& gameRunning, SDL_Event& event, GameSt
539570
window.render(numMisses);
540571
window.display();
541572
}
573+
574+
// Clean up end screen textures to prevent memory leaks
575+
SDL_DestroyTexture(statsTexture);
576+
SDL_DestroyTexture(scoreTexture);
577+
SDL_DestroyTexture(numberTexture);
578+
SDL_DestroyTexture(hitsTexture);
579+
SDL_DestroyTexture(numHitsTexture);
580+
SDL_DestroyTexture(accuracyTexture);
581+
SDL_DestroyTexture(accPercentTexture);
582+
SDL_DestroyTexture(missTexture);
583+
SDL_DestroyTexture(numMissTexture);
584+
SDL_DestroyTexture(quitTexture);
585+
SDL_DestroyTexture(retryTexture);
586+
SDL_DestroyTexture(logoTexture);
587+
SDL_DestroyTexture(selectedTexture);
588+
SDL_DestroyTexture(logoCatTexture);
542589
}
543590

544591
std::string formatScore(int score)

0 commit comments

Comments
 (0)