[MPlayer-dev-eng] [PATCH] mplayer memory leaks

Alexander Strasser eclipse7 at gmx.net
Wed Jul 21 15:16:24 CEST 2004


Hi,
as suggested to me by the mkv devels on LinuxTag, I tested
MPlayer for mem leaks with valgrind.

There were plenty of detected memory leaks in it's
output some may not be real leaks because valgrind can be
fooled by optimizations.
But others as the ones this patch fixes are real ones.

So as a first start i eleminated the mem leaks caused by
false use of the get_path() function.

As a second step i wrote a little src documentation for
get_path() also attached.

  Alex (beastd)
-------------- next part --------------
Index: mplayer.c
===================================================================
RCS file: /cvsroot/mplayer/main/mplayer.c,v
retrieving revision 1.763
diff -u -r1.763 mplayer.c
--- mplayer.c	17 Jul 2004 12:47:12 -0000	1.763
+++ mplayer.c	21 Jul 2004 12:40:13 -0000
@@ -723,6 +723,7 @@
 int main(int argc,char* argv[]){
 
 
+char * mem_ptr;
 
 static demux_stream_t *d_audio=NULL;
 static demux_stream_t *d_video=NULL;
@@ -904,7 +905,7 @@
 
 // check codec.conf
 if(!codecs_file || !parse_codec_cfg(codecs_file)){
-  if(!parse_codec_cfg(get_path("codecs.conf"))){
+  if(!parse_codec_cfg(mem_ptr=get_path("codecs.conf"))){
     if(!parse_codec_cfg(MPLAYER_CONFDIR "/codecs.conf")){
       if(!parse_codec_cfg(NULL)){
 	mp_msg(MSGT_CPLAYER,MSGL_HINT,MSGTR_CopyCodecsConf);
@@ -913,6 +914,7 @@
       mp_msg(MSGT_CPLAYER,MSGL_INFO,MSGTR_BuiltinCodecsConf);
     }
   }
+  free( mem_ptr ); // release the buffer created by get_path()
 }
 
 #if 0
@@ -1083,7 +1085,8 @@
        if(!vo_font) mp_msg(MSGT_CPLAYER,MSGL_ERR,MSGTR_CantLoadFont,font_name);
   } else {
       // try default:
-       vo_font=read_font_desc(get_path("font/font.desc"),font_factor,verbose>1);
+       vo_font=read_font_desc( mem_ptr=get_path("font/font.desc"),font_factor,verbose>1);
+       free(mem_ptr); // release the buffer created by get_path()
        if(!vo_font)
        vo_font=read_font_desc(MPLAYER_DATADIR "/font/font.desc",font_factor,verbose>1);
   }
@@ -1649,11 +1652,13 @@
     char *psub = get_path( "sub/" );
     char **tmp = sub_filenames((psub ? psub : ""), filename);
     char **tmp2 = tmp;
+    free(psub); // release the buffer created by get_path() above
     while (*tmp2)
         add_subtitles (*tmp2++, sh_video->fps, 0);
     free(tmp);
     if (set_of_sub_size == 0)
-        add_subtitles (get_path("default.sub"), sh_video->fps, 1);
+        add_subtitles (mem_ptr=get_path("default.sub"), sh_video->fps, 1);
+    free(mem_ptr); // release the buffer created by get_path()
     if (set_of_sub_size > 0)
         add_subtitles (NULL, sh_video->fps, 1);
   }
Index: input/input.c
===================================================================
RCS file: /cvsroot/mplayer/main/input/input.c,v
retrieving revision 1.90
diff -u -r1.90 input.c
--- input/input.c	17 Jul 2004 12:47:12 -0000	1.90
+++ input/input.c	21 Jul 2004 12:40:15 -0000
@@ -1484,7 +1484,10 @@
   if(!file)
     return;
   
-  if(! mp_input_parse_config(file)) {
+  if( mp_input_parse_config(file)) {
+    free(file); // release the buffer created by get_path()
+  }
+  else {
     // Try global conf dir
     file = MPLAYER_CONFDIR "/input.conf";
     if(! mp_input_parse_config(file))
-------------- next part --------------
Index: get_path.c
===================================================================
RCS file: /cvsroot/mplayer/main/get_path.c,v
retrieving revision 1.6
diff -u -r1.6 get_path.c
--- get_path.c	30 Apr 2004 16:56:00 -0000	1.6
+++ get_path.c	19 Jul 2004 16:13:16 -0000
@@ -1,4 +1,13 @@
 
+/*
+ * Get path to config dir/file.
+ *
+ * Return Values:
+ *   Returns the pointer to the ALLOCATED buffer containing the
+ *   zero terminated path string. This string has to be FREED
+ *   in the callers scope.
+ *
+ */
 char *get_path(char *filename){
 	char *homedir;
 	char *buff;


More information about the MPlayer-dev-eng mailing list