From 328ddd8a55fae55ea713d0538a46f58c50fb37b4 Mon Sep 17 00:00:00 2001 From: zicodxx <> Date: Thu, 2 Sep 2010 14:00:26 +0000 Subject: [PATCH] When opening music file via filehandle, made sure buffer is freed after playing to prevent major memory leakage --- CHANGELOG.txt | 1 + arch/sdl/digi_mixer_music.c | 24 ++++++++++++++++++++---- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/CHANGELOG.txt b/CHANGELOG.txt index bb9447c92..542eb3fb6 100644 --- a/CHANGELOG.txt +++ b/CHANGELOG.txt @@ -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 -------- diff --git a/arch/sdl/digi_mixer_music.c b/arch/sdl/digi_mixer_music.c index b2d7e14b9..2e5cb51bd 100644 --- a/arch/sdl/digi_mixer_music.c +++ b/arch/sdl/digi_mixer_music.c @@ -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()