From 6e7eb94c6867dd3efa80523f1bbf2da3a32c5db0 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Jun 2022 22:01:13 +0300 Subject: [PATCH 39/39] sdl2: Free city_map_canvas on exit Also have it originally initialized to NULL so that we never try to free memory from random address when reallocating the canvas See osdn #44755 Signed-off-by: Marko Lindqvist --- client/gui-sdl2/gui_main.c | 2 ++ client/gui-sdl2/mapview.c | 24 ++++++++++++++++-------- client/gui-sdl2/mapview.h | 1 + 3 files changed, 19 insertions(+), 8 deletions(-) diff --git a/client/gui-sdl2/gui_main.c b/client/gui-sdl2/gui_main.c index 4dbac53e18..8ce84dd208 100644 --- a/client/gui-sdl2/gui_main.c +++ b/client/gui-sdl2/gui_main.c @@ -1074,6 +1074,8 @@ void ui_exit(void) set_video_mode(320, 240, SDL_SWSURFACE | SDL_ANYFORMAT); #endif + city_map_canvas_free(); + free_mapcanvas_and_overview(); free_auxiliary_tech_icons(); diff --git a/client/gui-sdl2/mapview.c b/client/gui-sdl2/mapview.c index d7b764182d..c0aa89b10d 100644 --- a/client/gui-sdl2/mapview.c +++ b/client/gui-sdl2/mapview.c @@ -75,7 +75,7 @@ int overview_start_x = 0; int overview_start_y = 0; static struct canvas *overview_canvas; -static struct canvas *city_map_canvas; +static struct canvas *city_map_canvas = NULL; static struct canvas *terrain_canvas; /* ================================================================ */ @@ -1180,20 +1180,28 @@ void tileset_changed(void) ===================================================================== */ /**********************************************************************//** - Create new city map surface. + Free memory allocated for the city map canvas **************************************************************************/ -SDL_Surface *create_city_map(struct city *pcity) +void city_map_canvas_free(void) { - /* city map dimensions might have changed, so create a new canvas each time */ - - if (city_map_canvas) { + if (city_map_canvas != NULL) { canvas_free(city_map_canvas); + city_map_canvas = NULL; } +} + +/**********************************************************************//** + Create new city map surface. +**************************************************************************/ +SDL_Surface *create_city_map(struct city *pcity) +{ + /* City map dimensions might have changed, so create a new canvas each time */ + city_map_canvas_free(); - city_map_canvas = canvas_create(get_citydlg_canvas_width(), + city_map_canvas = canvas_create(get_citydlg_canvas_width(), get_citydlg_canvas_height()); - city_dialog_redraw_map(pcity, city_map_canvas); + city_dialog_redraw_map(pcity, city_map_canvas); return city_map_canvas->surf; } diff --git a/client/gui-sdl2/mapview.h b/client/gui-sdl2/mapview.h index e0b0ef31f7..7da7358e42 100644 --- a/client/gui-sdl2/mapview.h +++ b/client/gui-sdl2/mapview.h @@ -38,6 +38,7 @@ void redraw_unit_info_label(struct unit_list *punitlist); SDL_Surface *create_city_map(struct city *pcity); +void city_map_canvas_free(void); SDL_Surface *get_terrain_surface(struct tile *ptile); void refresh_overview(void); -- 2.35.1