Complete.Org: Mailing Lists: Archives: freeciv-dev: May 2005:
[Freeciv-Dev] (PR#13130) fix tilespec.c leak
Home

[Freeciv-Dev] (PR#13130) fix tilespec.c leak

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
Subject: [Freeciv-Dev] (PR#13130) fix tilespec.c leak
From: "Jason Dorje Short" <jdorje@xxxxxxxxx>
Date: Tue, 17 May 2005 15:29:11 -0700
Reply-to: bugs@xxxxxxxxxxx

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

This patch fixes a leak in tilespec.c.  It does so by adding the sprite
directly to the hash via a new function, insert_sprite.  This means the
sprite is freed automatically as part of the hash (and also that the
tileset author can make these sprites individually, as if anyone would
ever want to do that).

insert_sprite itself is pretty ugly.  But then so is tilespec.c.

-jason

? colors.diff
? diff
? common/team.c
? common/team.h
Index: client/tilespec.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/client/tilespec.c,v
retrieving revision 1.305
diff -u -r1.305 tilespec.c
--- client/tilespec.c   11 May 2005 20:35:29 -0000      1.305
+++ client/tilespec.c   17 May 2005 22:27:21 -0000
@@ -1659,6 +1659,28 @@
   }
 }
 
+/****************************************************************************
+  Insert a generated sprite into the existing sprite hash with a given
+  name.
+
+  This means the sprite can now be accessed via the tag, and will be
+  automatically freed along with the tileset.  In other words, you should
+  only do this with sprites you've just allocated, and only on tags that
+  are unused!
+****************************************************************************/
+static void insert_sprite(struct tileset *t, const char *tag_name,
+                         struct sprite *sprite)
+{
+  struct small_sprite *ss = fc_calloc(sizeof(*ss), 1);
+
+  assert(load_sprite(t, tag_name) == 0);
+  ss->ref_count = 1;
+  ss->sprite = sprite;
+  if (!hash_insert(t->sprite_hash, mystrdup(tag_name), ss)) {
+    freelog(LOG_ERROR, "warning: already have a sprite for %s", tag_name);
+  }
+}
+
 /**************************************************************************
   Return TRUE iff the specified sprite exists in the tileset (whether
   or not it is currently loaded).
@@ -2119,13 +2141,20 @@
        SET_SPRITE(grid.borders[i][j], buffer);
 
        for (p = 0; p < MAX_NUM_PLAYERS + MAX_NUM_BARBARIANS; p++) {
-         if (t->sprites.colors.player[p] && t->sprites.grid.borders[i][j]) {
-           s = crop_sprite(t->sprites.colors.player[p],
-                           0, 0,
-                           t->normal_tile_width, t->normal_tile_height,
-                           t->sprites.grid.borders[i][j], 0, 0);
-         } else {
-           s = t->sprites.grid.borders[i][j];
+         my_snprintf(buffer, sizeof(buffer), "grid.borders.%c.%d",
+                     name[i][j], p);
+         s = load_sprite(t, buffer);
+
+         if (!s) {
+           if (t->sprites.colors.player[p] && t->sprites.grid.borders[i][j]) {
+             s = crop_sprite(t->sprites.colors.player[p],
+                             0, 0,
+                             t->normal_tile_width, t->normal_tile_height,
+                             t->sprites.grid.borders[i][j], 0, 0);
+             insert_sprite(t, buffer, s);
+           } else {
+             s = t->sprites.grid.borders[i][j];
+           }
          }
          t->sprites.grid.player_borders[p][i][j] = s;
        }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#13130) fix tilespec.c leak, Jason Dorje Short <=