From 7f07ed8a5630185252df378aeb27387cafd6d9d7 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Jun 2022 22:05:24 +0300 Subject: [PATCH 18/18] 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 ce5d59aadd..502e7bd11a 100644 --- a/client/gui-sdl2/gui_main.c +++ b/client/gui-sdl2/gui_main.c @@ -1071,6 +1071,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 ecb58a0b29..a56f6887ce 100644 --- a/client/gui-sdl2/mapview.c +++ b/client/gui-sdl2/mapview.c @@ -74,7 +74,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; /* ================================================================ */ @@ -1172,20 +1172,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