Skip to content
Permalink
Browse files

Fix memory leaks in timidity player

  • Loading branch information
pmandin committed Jul 4, 2005
1 parent 836db36 commit 4b6e8959bf1dc741dc78394f762a7bb582b246d8
Showing with 65 additions and 17 deletions.
  1. +5 −0 music.c
  2. +27 −7 timidity/common.c
  3. +1 −0 timidity/common.h
  4. +13 −6 timidity/instrum.c
  5. +16 −1 timidity/playmidi.c
  6. +2 −2 timidity/timidity.c
  7. +1 −1 timidity/timidity.h
@@ -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
@@ -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,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);
@@ -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;
}
}
@@ -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);
@@ -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;
}
}
}


@@ -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;
}
}
@@ -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();
}

@@ -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
@@ -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.