Skip to content

Commit

Permalink
Fix memory leaks in timidity player
Browse files Browse the repository at this point in the history
  • Loading branch information
pmandin committed Jul 4, 2005
1 parent 836db36 commit 4b6e895
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 17 deletions.
5 changes: 5 additions & 0 deletions music.c
Expand Up @@ -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
Expand Down
34 changes: 27 additions & 7 deletions timidity/common.c
Expand Up @@ -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 */
Expand Down Expand Up @@ -110,13 +104,22 @@ FILE *open_file(char *name, int decompress, int noise_mode)
FILE *fp;
PathList *plp=pathlist;
int l;
static int firsttime=1;

if (!name || !(*name))
{
ctl->cmsg(CMSG_ERROR, VERB_NORMAL, "Attempted to open nameless file.");
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);
Expand Down Expand Up @@ -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;
}
}
1 change: 1 addition & 0 deletions timidity/common.h
Expand Up @@ -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);
19 changes: 13 additions & 6 deletions timidity/instrum.c
Expand Up @@ -110,16 +110,23 @@ static void free_bank(int dr, int b)
int i;
ToneBank *bank=((dr) ? drumset[b] : tonebank[b]);
for (i=0; i<MAXPROG; i++)
{
if (bank->tone[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;
}
}
}


Expand All @@ -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;
}
}
Expand Down
17 changes: 16 additions & 1 deletion timidity/playmidi.c
Expand Up @@ -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;
Expand Down Expand Up @@ -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();
}

4 changes: 2 additions & 2 deletions timidity/timidity.c
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion timidity/timidity.h
Expand Up @@ -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);

0 comments on commit 4b6e895

Please sign in to comment.