← Back to team overview

widelands-dev team mailing list archive

[Merge] lp:~aber/widelands/bug984110 into lp:widelands

 

David Allwicher has proposed merging lp:~aber/widelands/bug984110 into lp:widelands.

Requested reviews:
  Widelands Developers (widelands-dev)
Related bugs:
  Bug #984110 in widelands: "memory leak in src/ai/defaultai.cc:1439"
  https://bugs.launchpad.net/widelands/+bug/984110

For more details, see:
https://code.launchpad.net/~aber/widelands/bug984110/+merge/103729
-- 
https://code.launchpad.net/~aber/widelands/bug984110/+merge/103729
Your team Widelands Developers is requested to review the proposed merge of lp:~aber/widelands/bug984110 into lp:widelands.
=== modified file 'src/ai/defaultai.cc'
--- src/ai/defaultai.cc	2012-04-04 14:06:44 +0000
+++ src/ai/defaultai.cc	2012-04-26 16:23:01 +0000
@@ -1412,7 +1412,7 @@
 }
 
 
-/// connects a specific flag to another economy
+// connects a specific flag to another economy
 bool DefaultAI::connect_flag_to_another_economy (const Flag & flag)
 {
 	FindNodeWithFlagOrRoad functor;
@@ -1431,20 +1431,22 @@
 		return false;
 
 	// then choose the one with the shortest path
-	Path & path = *new Path();;
+	Path * path = new Path();
 	bool found = false;
 	check.set_openend(false);
 	Coords closest;
 	container_iterate_const(std::vector<Coords>, reachable, i) {
-		Path & path2 = *new Path();
-		if (map.findpath(flag.get_position(), *i.current, 0, path2, check) < 0)
-			continue;
-
-		if (!found || path.get_nsteps() > path2.get_nsteps()) {
-			path = path2;
-			closest = *i.current;
-			found = true;
+		Path * path2 = new Path();
+		if (map.findpath(flag.get_position(), *i.current, 0, *path2, check) >= 0) {
+			if (!found || path->get_nsteps() > path2->get_nsteps()) {
+				delete path;
+				path = path2;
+				path2 = NULL;
+				closest = *i.current;
+				found = true;
+			}
 		}
+		delete path2;
 	}
 
 	if (found) {
@@ -1453,10 +1455,12 @@
 			game().send_player_build_flag(player_number(), closest);
 
 		// and finally build the road
-		game().send_player_build_road(player_number(), path);
+		game().send_player_build_road(player_number(), *path);
 		return true;
+	} else {
+		delete path;
+		return false;
 	}
-	return false;
 }
 
 /// adds alternative ways to already existing ones


Follow ups