Fix memory leaks in timidity player
authorPatrice Mandin <patmandin@gmail.com>
Mon, 04 Jul 2005 19:45:30 +0000
changeset 264e62571f9bcbf
parent 263 5cd1ec721824
child 265 26629b8f3106
Fix memory leaks in timidity player
music.c
timidity/common.c
timidity/common.h
timidity/instrum.c
timidity/playmidi.c
timidity/timidity.c
timidity/timidity.h
     1.1 --- a/music.c	Mon Jul 04 19:37:59 2005 +0000
     1.2 +++ b/music.c	Mon Jul 04 19:45:30 2005 +0000
     1.3 @@ -1227,6 +1227,11 @@
     1.4  	MikMod_UnregisterAllDrivers();
     1.5  # endif
     1.6  #endif
     1.7 +#ifdef MID_MUSIC
     1.8 +# ifdef USE_TIMIDITY_MIDI
     1.9 +	Timidity_Close();
    1.10 +# endif
    1.11 +#endif
    1.12  }
    1.13  
    1.14  # ifdef LIBMIKMOD_MUSIC
     2.1 --- a/timidity/common.c	Mon Jul 04 19:37:59 2005 +0000
     2.2 +++ b/timidity/common.c	Mon Jul 04 19:45:30 2005 +0000
     2.3 @@ -36,13 +36,7 @@
     2.4  
     2.5  char current_filename[1024];
     2.6  
     2.7 -#ifdef DEFAULT_PATH
     2.8 -    /* The paths in this list will be tried whenever we're reading a file */
     2.9 -    static PathList defaultpathlist={DEFAULT_PATH,0};
    2.10 -    static PathList *pathlist=&defaultpathlist; /* This is a linked list */
    2.11 -#else
    2.12 -    static PathList *pathlist=0;
    2.13 -#endif
    2.14 +static PathList *pathlist=NULL;
    2.15  
    2.16  /* Try to open a file for reading. If the filename ends in one of the 
    2.17     defined compressor extensions, pipe the file through the decompressor */
    2.18 @@ -110,6 +104,7 @@
    2.19    FILE *fp;
    2.20    PathList *plp=pathlist;
    2.21    int l;
    2.22 +  static int firsttime=1;
    2.23  
    2.24    if (!name || !(*name))
    2.25      {
    2.26 @@ -117,6 +112,14 @@
    2.27        return 0;
    2.28      }
    2.29  
    2.30 +#ifdef DEFAULT_PATH
    2.31 +  if (firsttime && (pathlist==NULL)) {
    2.32 +    /* Generate path list */
    2.33 +    add_to_pathlist(DEFAULT_PATH);
    2.34 +    firsttime=0;
    2.35 +  }
    2.36 +#endif
    2.37 +
    2.38    /* First try the given name */
    2.39  
    2.40    strncpy(current_filename, name, 1023);
    2.41 @@ -224,3 +227,20 @@
    2.42    plp->next=pathlist;
    2.43    pathlist=plp;
    2.44  }
    2.45 +
    2.46 +/* Free memory associated to path list */
    2.47 +void free_pathlist(void)
    2.48 +{
    2.49 +  PathList *plp, *next_plp;
    2.50 +
    2.51 +  plp = pathlist;
    2.52 +  while (plp) {
    2.53 +    if (plp->path) {
    2.54 +      free(plp->path);
    2.55 +      plp->path=NULL;
    2.56 +    }
    2.57 +    next_plp = plp->next;
    2.58 +    free(plp);
    2.59 +    plp = next_plp;
    2.60 +  }
    2.61 +}
     3.1 --- a/timidity/common.h	Mon Jul 04 19:37:59 2005 +0000
     3.2 +++ b/timidity/common.h	Mon Jul 04 19:45:30 2005 +0000
     3.3 @@ -42,6 +42,7 @@
     3.4  
     3.5  extern FILE *open_file(char *name, int decompress, int noise_mode);
     3.6  extern void add_to_pathlist(char *s);
     3.7 +extern void free_pathlist(void);
     3.8  extern void close_file(FILE *fp);
     3.9  extern void skip(FILE *fp, size_t len);
    3.10  extern void *safe_malloc(size_t count);
     4.1 --- a/timidity/instrum.c	Mon Jul 04 19:37:59 2005 +0000
     4.2 +++ b/timidity/instrum.c	Mon Jul 04 19:45:30 2005 +0000
     4.3 @@ -110,16 +110,23 @@
     4.4    int i;
     4.5    ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
     4.6    for (i=0; i<MAXPROG; i++)
     4.7 +  {
     4.8      if (bank->tone[i].layer)
     4.9 -      {
    4.10 -	/* Not that this could ever happen, of course */
    4.11 -	if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
    4.12 +    {
    4.13 +	  /* Not that this could ever happen, of course */
    4.14 +	  if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT)
    4.15  	  {
    4.16  	    free_layer(bank->tone[i].layer);
    4.17 -	    bank->tone[i].layer=0;
    4.18 +	    bank->tone[i].layer=NULL;
    4.19  	    bank->tone[i].last_used=-1;
    4.20  	  }
    4.21 -      }
    4.22 +    }
    4.23 +    if (bank->tone[i].name)
    4.24 +    {
    4.25 +      free(bank->tone[i].name);
    4.26 +      bank->tone[i].name = NULL;
    4.27 +    }
    4.28 +  }
    4.29  }
    4.30  
    4.31  
    4.32 @@ -137,7 +144,7 @@
    4.33  		(dr)? "drum" : "inst", bank->tone[i].name,
    4.34  		i, b, bank->tone[i].last_used);
    4.35  	    free_layer(bank->tone[i].layer);
    4.36 -	    bank->tone[i].layer=0;
    4.37 +	    bank->tone[i].layer=NULL;
    4.38  	    bank->tone[i].last_used=-1;
    4.39  	  }
    4.40        }
     5.1 --- a/timidity/playmidi.c	Mon Jul 04 19:37:59 2005 +0000
     5.2 +++ b/timidity/playmidi.c	Mon Jul 04 19:45:30 2005 +0000
     5.3 @@ -81,6 +81,7 @@
     5.4  static int32 *buffer_pointer;
     5.5  static int32 buffered_count;
     5.6  extern int32 *common_buffer;
     5.7 +extern resample_t *resample_buffer; /* to free it on Timidity_Close */
     5.8  
     5.9  static MidiEvent *event_list, *current_event;
    5.10  static int32 sample_count, current_sample;
    5.11 @@ -1749,9 +1750,23 @@
    5.12  void Timidity_FreeSong(MidiSong *song)
    5.13  {
    5.14    if (free_instruments_afterwards)
    5.15 -      free_instruments();
    5.16 +    free_instruments();
    5.17    
    5.18    free(song->events);
    5.19    free(song);
    5.20  }
    5.21  
    5.22 +void Timidity_Close(void)
    5.23 +{
    5.24 +  if (resample_buffer) {
    5.25 +    free(resample_buffer);
    5.26 +    resample_buffer=NULL;
    5.27 +  }
    5.28 +  if (common_buffer) {
    5.29 +    free(common_buffer);
    5.30 +    common_buffer=NULL;
    5.31 +  }
    5.32 +  free_instruments();
    5.33 +  free_pathlist();
    5.34 +}
    5.35 +
     6.1 --- a/timidity/timidity.c	Mon Jul 04 19:37:59 2005 +0000
     6.2 +++ b/timidity/timidity.c	Mon Jul 04 19:45:30 2005 +0000
     6.3 @@ -39,8 +39,8 @@
     6.4  static char def_instr_name[256]="";
     6.5  
     6.6  int AUDIO_BUFFER_SIZE;
     6.7 -resample_t *resample_buffer;
     6.8 -int32 *common_buffer;
     6.9 +resample_t *resample_buffer=NULL;
    6.10 +int32 *common_buffer=NULL;
    6.11  int num_ochannels;
    6.12  
    6.13  #define MAXWORDS 10
     7.1 --- a/timidity/timidity.h	Mon Jul 04 19:37:59 2005 +0000
     7.2 +++ b/timidity/timidity.h	Mon Jul 04 19:45:30 2005 +0000
     7.3 @@ -30,4 +30,4 @@
     7.4  extern int Timidity_Active(void);
     7.5  extern void Timidity_Stop(void);
     7.6  extern void Timidity_FreeSong(MidiSong *song);
     7.7 -
     7.8 +extern void Timidity_Close(void);