From 4b6e8959bf1dc741dc78394f762a7bb582b246d8 Mon Sep 17 00:00:00 2001 From: Patrice Mandin Date: Mon, 4 Jul 2005 19:45:30 +0000 Subject: [PATCH] Fix memory leaks in timidity player --- music.c | 5 +++++ timidity/common.c | 34 +++++++++++++++++++++++++++------- timidity/common.h | 1 + timidity/instrum.c | 19 +++++++++++++------ timidity/playmidi.c | 17 ++++++++++++++++- timidity/timidity.c | 4 ++-- timidity/timidity.h | 2 +- 7 files changed, 65 insertions(+), 17 deletions(-) diff --git a/music.c b/music.c index 0ff03494..da52ee29 100644 --- a/music.c +++ b/music.c @@ -1227,6 +1227,11 @@ void close_music(void) MikMod_UnregisterAllDrivers(); # endif #endif +#ifdef MID_MUSIC +# ifdef USE_TIMIDITY_MIDI + Timidity_Close(); +# endif +#endif } # ifdef LIBMIKMOD_MUSIC diff --git a/timidity/common.c b/timidity/common.c index b33f21cb..7fd23809 100644 --- a/timidity/common.c +++ b/timidity/common.c @@ -36,13 +36,7 @@ char current_filename[1024]; -#ifdef DEFAULT_PATH - /* The paths in this list will be tried whenever we're reading a file */ - static PathList defaultpathlist={DEFAULT_PATH,0}; - static PathList *pathlist=&defaultpathlist; /* This is a linked list */ -#else - static PathList *pathlist=0; -#endif +static PathList *pathlist=NULL; /* Try to open a file for reading. If the filename ends in one of the defined compressor extensions, pipe the file through the decompressor */ @@ -110,6 +104,7 @@ FILE *open_file(char *name, int decompress, int noise_mode) FILE *fp; PathList *plp=pathlist; int l; + static int firsttime=1; if (!name || !(*name)) { @@ -117,6 +112,14 @@ FILE *open_file(char *name, int decompress, int noise_mode) return 0; } +#ifdef DEFAULT_PATH + if (firsttime && (pathlist==NULL)) { + /* Generate path list */ + add_to_pathlist(DEFAULT_PATH); + firsttime=0; + } +#endif + /* First try the given name */ strncpy(current_filename, name, 1023); @@ -224,3 +227,20 @@ void add_to_pathlist(char *s) plp->next=pathlist; pathlist=plp; } + +/* Free memory associated to path list */ +void free_pathlist(void) +{ + PathList *plp, *next_plp; + + plp = pathlist; + while (plp) { + if (plp->path) { + free(plp->path); + plp->path=NULL; + } + next_plp = plp->next; + free(plp); + plp = next_plp; + } +} diff --git a/timidity/common.h b/timidity/common.h index 3b3ae239..362fc53d 100644 --- a/timidity/common.h +++ b/timidity/common.h @@ -42,6 +42,7 @@ typedef struct { extern FILE *open_file(char *name, int decompress, int noise_mode); extern void add_to_pathlist(char *s); +extern void free_pathlist(void); extern void close_file(FILE *fp); extern void skip(FILE *fp, size_t len); extern void *safe_malloc(size_t count); diff --git a/timidity/instrum.c b/timidity/instrum.c index ee9242a0..71da70ab 100644 --- a/timidity/instrum.c +++ b/timidity/instrum.c @@ -110,16 +110,23 @@ static void free_bank(int dr, int b) int i; ToneBank *bank=((dr) ? drumset[b] : tonebank[b]); for (i=0; itone[i].layer) - { - /* Not that this could ever happen, of course */ - if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT) + { + /* Not that this could ever happen, of course */ + if (bank->tone[i].layer != MAGIC_LOAD_INSTRUMENT) { free_layer(bank->tone[i].layer); - bank->tone[i].layer=0; + bank->tone[i].layer=NULL; bank->tone[i].last_used=-1; } - } + } + if (bank->tone[i].name) + { + free(bank->tone[i].name); + bank->tone[i].name = NULL; + } + } } @@ -137,7 +144,7 @@ static void free_old_bank(int dr, int b, int how_old) (dr)? "drum" : "inst", bank->tone[i].name, i, b, bank->tone[i].last_used); free_layer(bank->tone[i].layer); - bank->tone[i].layer=0; + bank->tone[i].layer=NULL; bank->tone[i].last_used=-1; } } diff --git a/timidity/playmidi.c b/timidity/playmidi.c index 876692af..ad54575d 100644 --- a/timidity/playmidi.c +++ b/timidity/playmidi.c @@ -81,6 +81,7 @@ static int32 lost_notes, cut_notes; static int32 *buffer_pointer; static int32 buffered_count; extern int32 *common_buffer; +extern resample_t *resample_buffer; /* to free it on Timidity_Close */ static MidiEvent *event_list, *current_event; static int32 sample_count, current_sample; @@ -1749,9 +1750,23 @@ void Timidity_Stop(void) void Timidity_FreeSong(MidiSong *song) { if (free_instruments_afterwards) - free_instruments(); + free_instruments(); free(song->events); free(song); } +void Timidity_Close(void) +{ + if (resample_buffer) { + free(resample_buffer); + resample_buffer=NULL; + } + if (common_buffer) { + free(common_buffer); + common_buffer=NULL; + } + free_instruments(); + free_pathlist(); +} + diff --git a/timidity/timidity.c b/timidity/timidity.c index 86719bc1..f5bb87d9 100644 --- a/timidity/timidity.c +++ b/timidity/timidity.c @@ -39,8 +39,8 @@ int free_instruments_afterwards=0; static char def_instr_name[256]=""; int AUDIO_BUFFER_SIZE; -resample_t *resample_buffer; -int32 *common_buffer; +resample_t *resample_buffer=NULL; +int32 *common_buffer=NULL; int num_ochannels; #define MAXWORDS 10 diff --git a/timidity/timidity.h b/timidity/timidity.h index 2cffcc99..60281578 100644 --- a/timidity/timidity.h +++ b/timidity/timidity.h @@ -30,4 +30,4 @@ extern void Timidity_Start(MidiSong *song); extern int Timidity_Active(void); extern void Timidity_Stop(void); extern void Timidity_FreeSong(MidiSong *song); - +extern void Timidity_Close(void);