When opening music file via filehandle, made sure buffer is freed after playing to prevent major memory leakage

This commit is contained in:
zicodxx 2010-09-02 14:00:26 +00:00
parent 5e3be36c39
commit 328ddd8a55
2 changed files with 21 additions and 4 deletions

View file

@ -5,6 +5,7 @@ D1X-Rebirth Changelog
arch/sdl/digi_mixer.c, arch/sdl/digi_mixer_music.c, arch/sdl/jukebox.c, d1x-rebirth.xcodeproj/project.pbxproj, main/multi.h: On Mac OS X - no longer have to copy SDL_mixer.h to SDL framework; frameworks can now be in /Library/Frameworks; fix for obscure compile error involving u_int32_t
include/ogl_init.h, 2d/font.c, arch/ogl/ogl.c, arch/ogl/gr.c: Rewrote code to control Texture Filtering a little so it's easier to apply Mipmaps for different parts of the game independently
main/gamecntl.c, main/menu.c, main/newdemo.c, main/newdemo.h: Using PHYSFSX_findFiles to make sure random demo playback will only find actual demo files and not quit autodemo; added DEMO_EXT for an universal definition of demo file extension
arch/sdl/digi_mixer_music.c: When opening music file via filehandle, made sure buffer is freed after playing to prevent major memory leakage
20100901
--------

View file

@ -23,6 +23,7 @@
Mix_Music *current_music = NULL;
char *current_music_hndlbuf = NULL;
/*
* Plays a music file from an absolute path or a relative path
@ -33,7 +34,7 @@ int mix_play_file(char *filename, int loop, void (*hook_finished_track)())
SDL_RWops *rw = NULL;
PHYSFS_file *filehandle = NULL;
char midi_filename[PATH_MAX], full_path[PATH_MAX];
char *basedir = "music", *fptr, *buf = NULL;
char *basedir = "music", *fptr;
int bufsize = 0;
mix_free_music(); // stop and free what we're already playing, if anything
@ -72,9 +73,9 @@ int mix_play_file(char *filename, int loop, void (*hook_finished_track)())
filehandle = PHYSFS_openRead(filename);
if (filehandle != NULL)
{
buf = realloc(buf, sizeof(char *)*PHYSFS_fileLength(filehandle));
bufsize = PHYSFS_read(filehandle, buf, sizeof(char), PHYSFS_fileLength(filehandle));
rw = SDL_RWFromConstMem(buf,bufsize*sizeof(char));
current_music_hndlbuf = d_realloc(current_music_hndlbuf, sizeof(char *)*PHYSFS_fileLength(filehandle));
bufsize = PHYSFS_read(filehandle, current_music_hndlbuf, sizeof(char), PHYSFS_fileLength(filehandle));
rw = SDL_RWFromConstMem(current_music_hndlbuf,bufsize*sizeof(char));
PHYSFS_close(filehandle);
current_music = Mix_LoadMUS_RW(rw);
}
@ -89,6 +90,11 @@ int mix_play_file(char *filename, int loop, void (*hook_finished_track)())
{
con_printf(CON_CRITICAL,"Music %s could not be loaded\n", filename);
Mix_HaltMusic();
if (current_music_hndlbuf)
{
d_free(current_music_hndlbuf);
current_music_hndlbuf = NULL;
}
}
return 0;
@ -103,6 +109,11 @@ void mix_free_music()
Mix_FreeMusic(current_music);
current_music = NULL;
}
if (current_music_hndlbuf)
{
d_free(current_music_hndlbuf);
current_music_hndlbuf = NULL;
}
}
void mix_set_music_volume(int vol)
@ -114,6 +125,11 @@ void mix_set_music_volume(int vol)
void mix_stop_music()
{
Mix_HaltMusic();
if (current_music_hndlbuf)
{
d_free(current_music_hndlbuf);
current_music_hndlbuf = NULL;
}
}
void mix_pause_music()