From 8f5e755c0910107760dea982018aa33820e42e54 Mon Sep 17 00:00:00 2001 From: Vicki Pfau Date: Fri, 13 May 2022 00:04:16 -0700 Subject: [PATCH] GB, GBA Savedata: Fix memory leaks when loading new saves --- src/gb/gb.c | 3 +++ src/gba/gba.c | 2 ++ src/gba/savedata.c | 3 +++ 3 files changed, 8 insertions(+) diff --git a/src/gb/gb.c b/src/gb/gb.c index 0582fff70..edcd548d1 100644 --- a/src/gb/gb.c +++ b/src/gb/gb.c @@ -229,6 +229,9 @@ static void GBSramDeinit(struct GB* gb) { bool GBLoadSave(struct GB* gb, struct VFile* vf) { GBSramDeinit(gb); gb->sramVf = vf; + if (gb->sramRealVf && gb->sramRealVf != vf) { + gb->sramRealVf->close(gb->sramRealVf); + } gb->sramRealVf = vf; if (gb->sramSize) { GBResizeSram(gb, gb->sramSize); diff --git a/src/gba/gba.c b/src/gba/gba.c index 4bf7721c8..9714d2c51 100644 --- a/src/gba/gba.c +++ b/src/gba/gba.c @@ -75,6 +75,8 @@ static void GBAInit(void* cpu, struct mCPUComponent* component) { GBAMemoryInit(gba); gba->memory.savedata.timing = &gba->timing; + gba->memory.savedata.vf = NULL; + gba->memory.savedata.realVf = NULL; GBASavedataInit(&gba->memory.savedata, NULL); gba->video.p = gba; diff --git a/src/gba/savedata.c b/src/gba/savedata.c index 182a6cc30..752d3874c 100644 --- a/src/gba/savedata.c +++ b/src/gba/savedata.c @@ -45,6 +45,9 @@ void GBASavedataInit(struct GBASavedata* savedata, struct VFile* vf) { savedata->command = EEPROM_COMMAND_NULL; savedata->flashState = FLASH_STATE_RAW; savedata->vf = vf; + if (savedata->realVf && savedata->realVf != vf) { + savedata->realVf->close(savedata->realVf); + } savedata->realVf = vf; savedata->mapMode = MAP_WRITE; savedata->maskWriteback = false;