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

Subject: duplicate tileset tags cause a memory leak
Date: Mon, 07 Feb 2005 20:50:17 -0500
To: The default queue
From: Jason Dorje Short

This is in 2.0 and 2.0.99.

The code is in tilespec.c:

for (k = 0; k < num_tags; k++) {
(void) hash_replace(sprite_hash, mystrdup(tags[k]), ss);
}
}

this leads to lots of leaked memory from the mystrdup call, when civ3gfx
(which has many duplicate tags) is used.

-------

 Patch


 - ML

diff -Nurd -X.diff_ignore freeciv/client/tilespec.c freeciv/client/tilespec.c
--- freeciv/client/tilespec.c	2007-08-25 15:16:07.000000000 +0300
+++ freeciv/client/tilespec.c	2007-08-31 02:25:44.000000000 +0300
@@ -429,6 +429,13 @@
 
 int focus_unit_state = 0;
 
+/****************************************************************************
+  Hash callback for freeing key
+****************************************************************************/
+static void sprite_hash_free_key(void *key)
+{
+  free(key);
+}
 
 /****************************************************************************
   Return the name of the given tileset.
@@ -1593,7 +1600,8 @@
   }
 
   assert(t->sprite_hash == NULL);
-  t->sprite_hash = hash_new(hash_fval_string, hash_fcmp_string);
+  t->sprite_hash = hash_new_full(hash_fval_string, hash_fcmp_string,
+                                 sprite_hash_free_key, NULL);
   for (i = 0; i < num_spec_files; i++) {
     struct specfile *sf = fc_malloc(sizeof(*sf));
     char *dname;
@@ -4430,7 +4438,6 @@
       const char *key = hash_key_by_number(t->sprite_hash, 0);
 
       hash_delete_entry(t->sprite_hash, key);
-      free((void *) key);
     }
 
     hash_free(t->sprite_hash);
_______________________________________________
Freeciv-dev mailing list
Freeciv-dev@gna.org
https://mail.gna.org/listinfo/freeciv-dev

Reply via email to