Complete.Org: Mailing Lists: Archives: freeciv-dev: March 2005:
[Freeciv-Dev] Re: (PR#12443) memory leaks in CM
Home

[Freeciv-Dev] Re: (PR#12443) memory leaks in CM

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: per@xxxxxxxxxxx
Subject: [Freeciv-Dev] Re: (PR#12443) memory leaks in CM
From: "Benoit Hudson" <bh@xxxxxxxxxxxxxxxxxxx>
Date: Mon, 7 Mar 2005 16:14:40 -0800
Reply-to: bugs@xxxxxxxxxxx

<URL: http://bugs.freeciv.org/Ticket/Display.html?id=12443 >

On Mon, Mar 07, 2005 at 02:14:04PM -0800, Jason Short wrote:
> 
> <URL: http://bugs.freeciv.org/Ticket/Display.html?id=12443 >
> 
> This fix causes memory errors:

Oh, yes, it would.  Try this one:

- Correctly free removed types in clean_lattice
- Added comments to clean_lattice

Index: common/aicore/cm.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/aicore/cm.c,v
retrieving revision 1.58
diff -b -u -p -r1.58 cm.c
--- common/aicore/cm.c  7 Jan 2005 02:59:39 -0000       1.58
+++ common/aicore/cm.c  8 Mar 2005 00:10:54 -0000
@@ -1040,20 +1040,26 @@ static void clean_lattice(struct tile_ty
                          const struct city *pcity)
 {
   int i, j; /* i is the index we read, j is the index we write */
+  struct tile_type_vector tofree;
+
+  /* We collect the types we want to remove and free them in one fell 
+     swoop at the end, in order to avoid memory errors.  */
+  tile_type_vector_init(&tofree);
 
   for (i = 0, j = 0; i < lattice->size; i++) {
     struct cm_tile_type *ptype = lattice->p[i];
 
     if (ptype->lattice_depth >= pcity->size) {
-      tile_type_destroy(ptype);
+      tile_type_vector_add(&tofree, ptype);
     } else {
-      int ci, cj;
+      /* Remove links to children that are being removed. */
+
+      int ci, cj; /* 'c' for 'child'; read from ci, write to cj */
 
-      lattice->p[j] = lattice->p[i];
+      lattice->p[j] = ptype;
       lattice->p[j]->lattice_index = j;
       j++;
 
-      /* remove children with overly high depth as well */
       for (ci = 0, cj = 0; ci < ptype->worse_types.size; ci++) {
         const struct cm_tile_type *ptype2 = ptype->worse_types.p[ci];
 
@@ -1066,6 +1072,8 @@ static void clean_lattice(struct tile_ty
     }
   }
   lattice->size = j;
+
+  tile_type_vector_free_all(&tofree);
 }
 
 /****************************************************************************

[Prev in Thread] Current Thread [Next in Thread]