From 8e58e3ee0f806d54a8612fcfc5c5f4c435a90588 Mon Sep 17 00:00:00 2001 From: "Daniel T. Borelli" Date: Sat, 3 Sep 2022 10:37:44 -0300 Subject: [PATCH 1/2] Fix memory leak --- src/det_vesa.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/det_vesa.c b/src/det_vesa.c index fc150a8..328614e 100644 --- a/src/det_vesa.c +++ b/src/det_vesa.c @@ -50,6 +50,7 @@ void detectar_vesa(void) { // Detects available video modes } else { for(i=0;modes[i];++i) { modos[i].ancho=modes[i]->w; modos[i].alto=modes[i]->h; modos[i].modo=1; + free(modes[i]); } num_modos=i-1; From 296d010895414750fa1babdd0a80e673024da64d Mon Sep 17 00:00:00 2001 From: "Daniel T. Borelli" Date: Sat, 3 Sep 2022 11:50:16 -0300 Subject: [PATCH 2/2] Don't make a temporary variable static The 'smodes' variable is temporary so instead of being static it uses allocated memory. Note that the 'smodes' variable must be null-terminated. --- src/det_vesa.c | 1 + src/shared/osdep/osd_sdl12.c | 12 +++++------- 2 files changed, 6 insertions(+), 7 deletions(-) diff --git a/src/det_vesa.c b/src/det_vesa.c index 328614e..23e0f2b 100644 --- a/src/det_vesa.c +++ b/src/det_vesa.c @@ -53,6 +53,7 @@ void detectar_vesa(void) { // Detects available video modes free(modes[i]); } num_modos=i-1; + free(modes); } diff --git a/src/shared/osdep/osd_sdl12.c b/src/shared/osdep/osd_sdl12.c index 612fd6f..bee1d61 100644 --- a/src/shared/osdep/osd_sdl12.c +++ b/src/shared/osdep/osd_sdl12.c @@ -60,7 +60,7 @@ void OSDEP_SetCaption(char *title, char *icon) { OSDEP_VMode **OSDEP_ListModes(void) { SDL_Rect **modes; int i; - static OSDEP_VMode *smodes[1024]; + OSDEP_VMode **smodes; modes = SDL_ListModes(NULL, SDL_FULLSCREEN | SDL_HWSURFACE); if(modes == (SDL_Rect **)0) { @@ -71,15 +71,13 @@ OSDEP_VMode **OSDEP_ListModes(void) { return -1; } - for(i = 0; modes[i]; ++i) { - if(modes[i]) { + smodes = calloc(1024, sizeof(OSDEP_VMode *)); + + for(i = 0; i < 1023 && modes[i]; ++i) { + //Note: smodes[1023] reserved to NULL smodes[i] = (OSDEP_VMode *)malloc(sizeof(OSDEP_VMode)); smodes[i]->w = modes[i]->w; smodes[i]->h = modes[i]->h; - } - else { - smodes[i] = NULL; - } } return smodes;