Complete.Org: Mailing Lists: Archives: freeciv-dev: October 2004:
[Freeciv-Dev] (PR#10664) ai_data_init memory leak
Home

[Freeciv-Dev] (PR#10664) ai_data_init memory leak

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] (PR#10664) ai_data_init memory leak
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Thu, 21 Oct 2004 10:23:00 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=10664 >

==3280== 3416 bytes in 198 blocks are definitely lost in loss record 13 
of 16
==3280==    at 0x1B906EDD: malloc (vg_replace_malloc.c:131)
==3280==    by 0x804B512: fc_real_malloc (mem.c:79)
==3280==    by 0x804B607: fc_real_calloc (mem.c:124)
==3280==    by 0x811DECF: ai_data_init (aidata.c:446)
==3280==    by 0x8081C05: server_player_init (plrhand.c:1642)
==3280==    by 0x8053B74: create_ai_player (stdinhand.c:866)
==3280==    by 0x805B00F: handle_stdin_input (stdinhand.c:3353)
==3280==    by 0x8053E67: read_init_script (stdinhand.c:944)
==3280==    by 0x8053F1E: read_command (stdinhand.c:965)
==3280==    by 0x805B36B: handle_stdin_input (stdinhand.c:3398)
==3280==    by 0x80A050F: handle_readline_input_callback (sernet.c:173)
==3280==    by 0x1B928150: rl_callback_read_char (callback.c:123)
==3280==    by 0x80A1400: sniff_packets (sernet.c:566)
==3280==    by 0x8051E43: srv_loop (srv_main.c:1689)
==3280==    by 0x8051D4B: srv_main (srv_main.c:1647)
==3280==    by 0x804A94E: main (civserver.c:170)

This memory is not freed at the end of the game.  So if you run multiple 
games, leaked memory will pile up.  And some memory is leaked even if 
you don't.  It appears that this data (ai_data_init) is allocated twice 
and freed (ai_data_done) not at all.

Fixing this the "clean" way isn't all that easy, I think.  Because 
players can change so often we have to be very careful where we allocate 
and deallocate this data.  Fortunately the data itself is so simple, the 
problem can be fixed just by using realloc.

jason

Index: ai/aidata.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.c,v
retrieving revision 1.44.2.1
diff -u -r1.44.2.1 aidata.c
--- ai/aidata.c 20 Oct 2004 04:41:35 -0000      1.44.2.1
+++ ai/aidata.c 21 Oct 2004 17:20:54 -0000
@@ -443,7 +443,11 @@
   int i;
 
   ai->govt_reeval = 0;
-  ai->government_want = fc_calloc(game.government_count + 1, sizeof(int));
+  ai->government_want = fc_realloc(ai->government_want,
+                                  ((game.government_count + 1)
+                                   * sizeof(*ai->government_want)));
+  memset(ai->government_want, 0,
+        (game.government_count + 1) * sizeof(*ai->government_want));
 
   ai->diplomacy.target = NULL;
   ai->diplomacy.strategy = WIN_OPEN;
@@ -467,13 +471,3 @@
     ai->diplomacy.player_intel[i].warned_about_space = 0;
   }
 }
-
-/**************************************************************************
-  Deinitialize data
-**************************************************************************/
-void ai_data_done(struct player *pplayer)
-{
-  struct ai_data *ai = &aidata[pplayer->player_no];
-
-  free(ai->government_want);
-}
Index: ai/aidata.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/ai/aidata.h,v
retrieving revision 1.18
diff -u -r1.18 aidata.h
--- ai/aidata.h 23 Sep 2004 17:20:35 -0000      1.18
+++ ai/aidata.h 21 Oct 2004 17:20:54 -0000
@@ -145,7 +145,6 @@
 void ai_data_turn_done(struct player *pplayer);
 
 void ai_data_init(struct player *pplayer);
-void ai_data_done(struct player *pplayer);
 void ai_data_analyze_rulesets(struct player *pplayer);
 
 struct ai_data *ai_data_get(struct player *pplayer);
Index: server/plrhand.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/server/plrhand.c,v
retrieving revision 1.330.2.2
diff -u -r1.330.2.2 plrhand.c
--- server/plrhand.c    18 Oct 2004 16:04:27 -0000      1.330.2.2
+++ server/plrhand.c    21 Oct 2004 17:20:54 -0000
@@ -1669,7 +1669,6 @@
     }
   } conn_list_iterate_end;
 
-  ai_data_done(pplayer);
   game_remove_player(pplayer);
   game_renumber_players(pplayer->player_no);
 }

[Prev in Thread] Current Thread [Next in Thread]
  • [Freeciv-Dev] (PR#10664) ai_data_init memory leak, Jason Short <=