changeset 8299:8879221d0548 draft

(svn r11863) -Fix (r11848): One day I'll learn C++... Delete all items in a drop down list before deleting the list.
author peter1138 <peter1138@openttd.org>
date Tue, 15 Jan 2008 13:20:58 +0000
parents afe49e7caeda
children ae1baac75172
files src/widgets/dropdown.cpp
diffstat 1 files changed, 22 insertions(+), 3 deletions(-) [+]
line wrap: on
line diff
--- a/src/widgets/dropdown.cpp
+++ b/src/widgets/dropdown.cpp
@@ -30,6 +30,19 @@
 	return this->string;
 }
 
+/**
+ * Delete all items of a drop down list and the list itself
+ * @param list List to delete.
+ */
+static void DeleteDropDownList(DropDownList *list)
+{
+	for (DropDownList::iterator it = list->begin(); it != list->end(); ++it) {
+		DropDownListItem *item = *it;
+		delete item;
+	}
+	delete list;
+}
+
 struct dropdown_d {
 	WindowClass parent_wnd_class;
 	WindowNumber parent_wnd_num;
@@ -160,7 +173,7 @@
 				w2->InvalidateWidget(WP(w, dropdown_d).parent_button);
 			}
 
-			delete WP(w, dropdown_d).list;
+			DeleteDropDownList(WP(w, dropdown_d).list);
 		} break;
 	}
 }
@@ -172,7 +185,7 @@
 	DeleteWindowById(WC_DROPDOWN_MENU, 0);
 
 	if (is_dropdown_menu_shown) {
-		delete list;
+		DeleteDropDownList(list);
 		return;
 	}
 
@@ -252,6 +265,12 @@
 
 void ShowDropDownMenu(Window *w, const StringID *strings, int selected, int button, uint32 disabled_mask, uint32 hidden_mask)
 {
+	/* Don't create a new list if we're just closing an existing menu */
+	if (w->IsWidgetLowered(button)) {
+		DeleteWindowById(WC_DROPDOWN_MENU, 0);
+		return;
+	}
+
 	uint result = 0;
 	DropDownList *list = new DropDownList();
 
@@ -264,7 +283,7 @@
 
 	/* No entries in the list? */
 	if (list->size() == 0) {
-		delete list;
+		DeleteDropDownList(list);
 		return;
 	}