From fafe17bc08f3c14c9f728c018b350a117892002b Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 11 Jun 2022 22:05:24 +0300 Subject: [PATCH 9/9] sdl/2: 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-sdl/gui_main.c | 2 ++ client/gui-sdl/mapview.c | 25 ++++++++++++++++++------- client/gui-sdl/mapview.h | 3 ++- client/gui-sdl2/gui_main.c | 2 ++ client/gui-sdl2/mapview.c | 24 ++++++++++++++++-------- client/gui-sdl2/mapview.h | 1 + 6 files changed, 41 insertions(+), 16 deletions(-) diff --git a/client/gui-sdl/gui_main.c b/client/gui-sdl/gui_main.c index da9de0feb7..28c758dd8d 100644 --- a/client/gui-sdl/gui_main.c +++ b/client/gui-sdl/gui_main.c @@ -1072,6 +1072,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-sdl/mapview.c b/client/gui-sdl/mapview.c index 71510c6a32..25cfb4c05e 100644 --- a/client/gui-sdl/mapview.c +++ b/client/gui-sdl/mapview.c @@ -70,7 +70,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; /* ================================================================ */ @@ -1141,18 +1141,29 @@ void tileset_changed(void) City MAP ===================================================================== */ -SDL_Surface *create_city_map(struct city *pCity) +/************************************************************************** + Free memory allocated for the city map canvas +**************************************************************************/ +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-sdl/mapview.h b/client/gui-sdl/mapview.h index c97d068d55..e9dd505696 100644 --- a/client/gui-sdl/mapview.h +++ b/client/gui-sdl/mapview.h @@ -30,7 +30,8 @@ #include "unitlist.h" void redraw_unit_info_label(struct unit_list *punitlist); -SDL_Surface *create_city_map(struct city *pCity); +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); diff --git a/client/gui-sdl2/gui_main.c b/client/gui-sdl2/gui_main.c index 0e2d1c679a..ab1f31f64e 100644 --- a/client/gui-sdl2/gui_main.c +++ b/client/gui-sdl2/gui_main.c @@ -1081,6 +1081,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 de1c984311..4675b66467 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