(svn r1978) - Fix: Plug some memleaks; thanks Valgrind
authorDarkvater
Wed, 09 Mar 2005 19:48:20 +0000
changeset 1474 d83520edcda6
parent 1473 4be673c6c994
child 1475 b5cf1fc28304
(svn r1978) - Fix: Plug some memleaks; thanks Valgrind
engine.c
engine.h
sdl.c
station_gui.c
ttd.c
window.c
window.h
--- a/engine.c	Wed Mar 09 19:42:37 2005 +0000
+++ b/engine.c	Wed Mar 09 19:48:20 2005 +0000
@@ -56,7 +56,7 @@
 	uint i;
 	StringID old;
 
-	for(i=0; i!=TOTAL_NUM_ENGINES; i++) {
+	for (i = 0; i != TOTAL_NUM_ENGINES; i++) {
 		old = _engine_name_strings[i];
 		_engine_name_strings[i] = i + STR_8000_KIRBY_PAUL_TANK_STEAM;
 		DeleteName(old);
@@ -73,10 +73,10 @@
 
 static void SetupEngineNames(void)
 {
-	uint i;
+	StringID *name;
 
-	for(i=0; i!=TOTAL_NUM_ENGINES; i++)
-		_engine_name_strings[i] = STR_SV_EMPTY;
+	for (name = _engine_name_strings; name != endof(_engine_name_strings); name++)
+		*name = STR_SV_EMPTY;
 
 	DeleteCustomEngineNames();
 	LoadCustomEngineNames();
@@ -200,7 +200,7 @@
 	AdjustAvailAircraft();
 }
 
-uint32 _engine_refit_masks[256];
+uint32 _engine_refit_masks[TOTAL_NUM_ENGINES];
 
 
 // TODO: We don't support cargo-specific wagon overrides. Pretty exotic... ;-) --pasky
@@ -214,7 +214,7 @@
 static struct WagonOverrides {
 	int overrides_count;
 	struct WagonOverride *overrides;
-} _engine_wagon_overrides[256];
+} _engine_wagon_overrides[TOTAL_NUM_ENGINES];
 
 void SetWagonOverrideSprites(byte engine, struct SpriteGroup *group,
                              byte *train_id, int trains)
@@ -260,12 +260,12 @@
 }
 
 
-byte _engine_original_sprites[256];
+byte _engine_original_sprites[TOTAL_NUM_ENGINES];
 // 0 - 28 are cargos, 29 is default, 30 is the advert (purchase list)
 // (It isn't and shouldn't be like this in the GRF files since new cargo types
 // may appear in future - however it's more convenient to store it like this in
 // memory. --pasky)
-static struct SpriteGroup _engine_custom_sprites[256][NUM_CID];
+static struct SpriteGroup _engine_custom_sprites[TOTAL_NUM_ENGINES][NUM_CID];
 
 void SetCustomEngineSprites(byte engine, byte cargo, struct SpriteGroup *group)
 {
@@ -622,14 +622,22 @@
 	DoTriggerVehicle(veh, trigger, 0, true);
 }
 
-
-static char *_engine_custom_names[256];
+static char *_engine_custom_names[TOTAL_NUM_ENGINES];
 
 void SetCustomEngineName(int engine, const char *name)
 {
 	_engine_custom_names[engine] = strdup(name);
 }
 
+void UnInitNewgrEngines(void)
+{
+	char **i;
+	for (i = _engine_custom_names; i != endof(_engine_custom_names); i++) {
+		free(*i);
+		*i = NULL;
+	}
+}
+
 StringID GetCustomEngineName(int engine)
 {
 	if (!_engine_custom_names[engine])
--- a/engine.h	Wed Mar 09 19:42:37 2005 +0000
+++ b/engine.h	Wed Mar 09 19:48:20 2005 +0000
@@ -134,6 +134,7 @@
 void DeleteCustomEngineNames(void);
 
 bool IsEngineBuildable(uint engine, byte type);
+void UnInitNewgrEngines(void);
 
 enum {
 	NUM_NORMAL_RAIL_ENGINES = 54,
--- a/sdl.c	Wed Mar 09 19:42:37 2005 +0000
+++ b/sdl.c	Wed Mar 09 19:48:20 2005 +0000
@@ -330,7 +330,7 @@
 static bool CreateMainSurface(int w, int h)
 {
 	SDL_Surface *newscreen;
-	char *caption;
+	char caption[50];
 
 	GetAvailableVideoMode(&w, &h);
 
@@ -348,7 +348,7 @@
 	_sdl_screen = newscreen;
 	InitPalette();
 
-	caption = str_fmt("OpenTTD %s", _openttd_revision);
+	snprintf(caption, sizeof(caption), "OpenTTD %s", _openttd_revision);
 	SDL_CALL SDL_WM_SetCaption(caption, caption);
 	SDL_CALL SDL_ShowCursor(0);
 
--- a/station_gui.c	Wed Mar 09 19:42:37 2005 +0000
+++ b/station_gui.c	Wed Mar 09 19:48:20 2005 +0000
@@ -82,8 +82,7 @@
 	uint16 *i;
 
 	// reset #-of stations to 0 because ++ is used for value-assignment
-	for (i = _num_station_sort; i != endof(_num_station_sort); i++)
-		*i = 0;
+	memset(_num_station_sort, 0, sizeof(_num_station_sort));
 
 	/* Create array for sorting */
 	_station_sort = realloc(_station_sort, GetStationPoolSize() * sizeof(_station_sort[0]));
--- a/ttd.c	Wed Mar 09 19:42:37 2005 +0000
+++ b/ttd.c	Wed Mar 09 19:48:20 2005 +0000
@@ -447,6 +447,11 @@
 	free(_industry_sort);
 }
 
+static void UnInitializeGame(void)
+{
+	UnInitWindowSystem();
+	UnInitNewgrEngines();
+}
 
 static void LoadIntroGame(void)
 {
@@ -459,7 +464,7 @@
 	LoadStringWidthTable();
 
 	// Setup main window
-	InitWindowSystem();
+	ResetWindowSystem();
 	SetupColorsAndInitialWindow();
 
 	// Generate a world.
@@ -699,6 +704,7 @@
 
 	/* Close all and any open filehandles */
 	FioCloseAll();
+	UnInitializeGame();
 
 	return 0;
 }
@@ -725,7 +731,7 @@
 	GfxLoadSprites();
 
 	// Reinitialize windows
-	InitWindowSystem();
+	ResetWindowSystem();
 	LoadStringWidthTable();
 
 	SetupColorsAndInitialWindow();
@@ -757,7 +763,7 @@
 	GfxLoadSprites();
 
 	// Re-init the windowing system
-	InitWindowSystem();
+	ResetWindowSystem();
 
 	// Create toolbars
 	SetupColorsAndInitialWindow();
@@ -792,7 +798,7 @@
 	GfxLoadSprites();
 
 	// Reinitialize windows
-	InitWindowSystem();
+	ResetWindowSystem();
 	LoadStringWidthTable();
 
 	SetupColorsAndInitialWindow();
@@ -1327,7 +1333,7 @@
 	}
 
 	// Initialize windows
-	InitWindowSystem();
+	ResetWindowSystem();
 	SetupColorsAndInitialWindow();
 
 	w = FindWindowById(WC_MAIN_WINDOW, 0);
--- a/window.c	Wed Mar 09 19:42:37 2005 +0000
+++ b/window.c	Wed Mar 09 19:48:20 2005 +0000
@@ -360,11 +360,10 @@
 			index++;
 		}
 
-		w->widget = malloc(sizeof(Widget) * index);
+		w->widget = realloc(w->widget, sizeof(Widget) * index);
 		memcpy(w->widget, widget, sizeof(Widget) * index);
-	} else {
+	} else
 		w->widget = NULL;
-	}
 }
 
 Window *AllocateWindow(
@@ -427,6 +426,7 @@
 	w->vscroll.count = 0;
 	w->hscroll.pos = 0;
 	w->hscroll.count = 0;
+	w->widget = NULL;
 	AssignWidgetToWindow(w, widget);
 	w->resize.width = width;
 	w->resize.height = height;
@@ -672,6 +672,7 @@
 void InitWindowSystem(void)
 {
 	IConsoleClose();
+
 	memset(&_windows, 0, sizeof(_windows));
 	_last_window = _windows;
 	memset(_viewports, 0, sizeof(_viewports));
@@ -679,12 +680,28 @@
 	_no_scroll = 0;
 }
 
+void UnInitWindowSystem(void)
+{
+	Window *w;
+	// delete all malloced widgets
+	for (w = _windows; w != _last_window; w++) {
+		free(w->widget);
+		w->widget = NULL;
+	}
+}
+
+void ResetWindowSystem(void)
+{
+	UnInitWindowSystem();
+	InitWindowSystem();
+}
+
 static void DecreaseWindowCounters(void)
 {
 	Window *w;
 
 
-	for(w=_last_window; w != _windows;) {
+	for (w = _last_window; w != _windows;) {
 		--w;
 		// Unclick scrollbar buttons if they are pressed.
 		if (w->flags4 & (WF_SCROLL_DOWN | WF_SCROLL_UP)) {
@@ -694,7 +711,7 @@
 		CallWindowEventNP(w, WE_MOUSELOOP);
 	}
 
-	for(w=_last_window; w != _windows;) {
+	for (w = _last_window; w != _windows;) {
 		--w;
 
 		if (w->flags4&WF_TIMEOUT_MASK && !(--w->flags4&WF_TIMEOUT_MASK)) {
--- a/window.h	Wed Mar 09 19:42:37 2005 +0000
+++ b/window.h	Wed Mar 09 19:48:20 2005 +0000
@@ -563,6 +563,8 @@
 void DrawWindowViewport(Window *w);
 
 void InitWindowSystem(void);
+void UnInitWindowSystem(void);
+void ResetWindowSystem(void);
 int GetMenuItemIndex(Window *w, int x, int y);
 void MouseLoop(void);
 void UpdateWindows(void);