From 86995441857328fd2aa956dac38266f1548d0195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20K=C3=B6pcke?= Date: Wed, 30 Mar 2022 12:30:17 +0200 Subject: [PATCH 1/3] Interface: Fix memory leak on opening ROMs, added function to manually free ROMs --- desmume/src/frontend/interface/interface.cpp | 13 +++++++++++++ desmume/src/frontend/interface/interface.h | 3 +++ 2 files changed, 16 insertions(+) diff --git a/desmume/src/frontend/interface/interface.cpp b/desmume/src/frontend/interface/interface.cpp index bb731db2c..2437b3e56 100644 --- a/desmume/src/frontend/interface/interface.cpp +++ b/desmume/src/frontend/interface/interface.cpp @@ -39,6 +39,7 @@ #define SCREENS_PIXEL_SIZE 98304 volatile bool execute = false; +volatile bool rom_opened = false; TieredRegion hooked_regions [HOOK_COUNT]; std::map hooks[HOOK_COUNT]; @@ -97,11 +98,23 @@ EXPORTED void desmume_set_language(u8 lang) EXPORTED int desmume_open(const char *filename) { int i; + if (rom_opened) { + NDS_FreeROM(); + } clear_savestates(); i = NDS_LoadROM(filename); + if (i > 0) { + rom_opened = true; + } return i; } +EXPORTED void desmume_close() +{ + NDS_FreeROM(); + rom_opened = false; +} + EXPORTED void desmume_set_savetype(int type) { backup_setManualBackupType(type); } diff --git a/desmume/src/frontend/interface/interface.h b/desmume/src/frontend/interface/interface.h index d595423c4..1b825247e 100755 --- a/desmume/src/frontend/interface/interface.h +++ b/desmume/src/frontend/interface/interface.h @@ -71,7 +71,10 @@ EXPORTED void desmume_free(void); // 0 = Japanese, 1 = English, 2 = French, 3 = German, 4 = Italian, 5 = Spanish EXPORTED void desmume_set_language(u8 language); +// Opens a new ROM, if a ROM was already opened, a new one is opened and the old was is automatically free'd. EXPORTED int desmume_open(const char *filename); +// Frees and closes a ROM opened with desmume_open. +EXPORTED void desmume_close(); EXPORTED void desmume_set_savetype(int type); EXPORTED void desmume_pause(void); EXPORTED void desmume_resume(void); From dc02de0a4af98ccf8f600a6c2c3bbef254b91193 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20K=C3=B6pcke?= Date: Wed, 30 Mar 2022 18:33:04 +0200 Subject: [PATCH 2/3] Normalized whitespaces for changed lines --- desmume/src/frontend/interface/interface.cpp | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/desmume/src/frontend/interface/interface.cpp b/desmume/src/frontend/interface/interface.cpp index 2437b3e56..9eb14f0cb 100644 --- a/desmume/src/frontend/interface/interface.cpp +++ b/desmume/src/frontend/interface/interface.cpp @@ -98,21 +98,21 @@ EXPORTED void desmume_set_language(u8 lang) EXPORTED int desmume_open(const char *filename) { int i; - if (rom_opened) { - NDS_FreeROM(); - } + if (rom_opened) { + NDS_FreeROM(); + } clear_savestates(); i = NDS_LoadROM(filename); - if (i > 0) { - rom_opened = true; - } + if (i > 0) { + rom_opened = true; + } return i; } EXPORTED void desmume_close() { - NDS_FreeROM(); - rom_opened = false; + NDS_FreeROM(); + rom_opened = false; } EXPORTED void desmume_set_savetype(int type) { From 66c8f91a5e75a01f7a6d557b0dfe285f4bacfb23 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marco=20K=C3=B6pcke?= Date: Wed, 30 Mar 2022 20:17:48 +0200 Subject: [PATCH 3/3] Intrface: Fixes keyword for rom_opened --- desmume/src/frontend/interface/interface.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desmume/src/frontend/interface/interface.cpp b/desmume/src/frontend/interface/interface.cpp index 9eb14f0cb..4ba60554a 100644 --- a/desmume/src/frontend/interface/interface.cpp +++ b/desmume/src/frontend/interface/interface.cpp @@ -39,7 +39,7 @@ #define SCREENS_PIXEL_SIZE 98304 volatile bool execute = false; -volatile bool rom_opened = false; +static bool rom_opened = false; TieredRegion hooked_regions [HOOK_COUNT]; std::map hooks[HOOK_COUNT];