[MPlayer-dev-eng] [PATCH] Fix memleak in stream_dvb.c (and libmenu patch)

Andrew Calkin andrew.calkin at gmail.com
Fri Dec 14 18:50:56 CET 2007


Hi,

The following patch fixes a memleak in stream/stream_dvb.c, where
the dvb_config memory was never freed. It needed a slight rework
of libmenu's dvb menu, because it also called on get_dvb_config(),
so I modified that too. The patches are attached, plz review and
comment.

//Andrew
-------------- next part --------------
--- MPlayer/stream/stream_dvb.c	2007-12-14 14:23:05.000000000 +0900
+++ MPlayer/stream/stream_dvb.c	2007-12-14 15:30:13.000000000 +0900
@@ -605,6 +605,8 @@
 
 	priv->is_on = 0;
 	dvb_config->priv = NULL;
+	dvb_free_config(dvb_config);
+	dvb_config = NULL;
 }
 
 
@@ -752,7 +754,7 @@
 	dvb_config_t *conf = NULL;
 	
 	if(dvb_config != NULL)
-		return dvb_config;
+		return dvb_dup_config(dvb_config);
 			
 	conf = malloc(sizeof(dvb_config_t));
 	if(conf == NULL)
@@ -840,10 +842,75 @@
 		conf = NULL;
 	}
 
-	dvb_config = conf;
 	return conf;
 }
 
+void dvb_free_config(dvb_config_t *config)
+{
+  int i;
+
+  if (!config)
+    return;
+
+  if (config->count > 0)
+    for (i=0; i<config->count; i++) 
+    {
+      if (config->cards[i].name)
+        free(config->cards[i].name);
+      if (config->cards[i].list)
+      {
+        if (config->cards[i].list->channels)
+        {
+          if (config->cards[i].list->channels->name)
+            free(config->cards[i].list->channels->name);
+          free(config->cards[i].list->channels);
+        }
+        free(config->cards[i].list);
+      }
+    }  
+  free(config);
+}
+
+dvb_config_t *dvb_dup_config(dvb_config_t *config)
+{
+  int i;
+  dvb_config_t *new_config;
+
+  if(!config || config->count == 0)
+    return NULL;
+
+  new_config = calloc(1, sizeof(dvb_config_t));
+  if (!new_config)
+    return NULL;
+
+  new_config->count = config->count;
+  new_config->cards = calloc(config->count, sizeof(dvb_card_config_t));
+
+  for (i=0; i<new_config->count; i++)
+  {
+    new_config->cards[i] = config->cards[i];
+    if (config->cards[i].name)
+      new_config->cards[i].name = strdup(config->cards[i].name);
+    if (config->cards[i].list)
+    {
+      new_config->cards[i].list = malloc(sizeof(dvb_channels_list));
+      if (!new_config->cards[i].list)
+        continue;
+      memcpy(new_config->cards[i].list, config->cards[i].list,
+             sizeof(dvb_channels_list));
+      if (config->cards[i].list->channels)
+      {
+        new_config->cards[i].list->channels = malloc(sizeof(dvb_channel_t));
+        if (!new_config->cards[i].list->channels)
+          continue;
+        memcpy(new_config->cards[i].list->channels,
+               config->cards[i].list->channels,
+               sizeof(dvb_channel_t));
+      }
+    }
+  }
+  return new_config;
+}
 
 
 const stream_info_t stream_info_dvb = {
--- MPlayer/stream/dvbin.h	2007-08-28 11:31:18.000000000 +0800
+++ MPlayer/stream/dvbin.h	2007-12-14 14:55:22.000000000 +0900
@@ -112,5 +112,7 @@
 extern int dvb_step_channel(dvb_priv_t *, int);
 extern int dvb_set_channel(dvb_priv_t *, int, int);
 extern dvb_config_t *dvb_get_config(void);
+extern void dvb_free_config(dvb_config_t *);
+extern dvb_config_t *dvb_dup_config(dvb_config_t *);
 
 #endif
-------------- next part --------------
--- MPlayer-r25315/libmenu/menu_dvbin.c	2007-12-14 15:34:15.000000000 +0900
+++ MPlayer-r25315/libmenu/menu_dvbin.c	2007-12-14 15:41:55.000000000 +0900
@@ -248,6 +248,7 @@
 
 static void close_menu(menu_t* menu)
 {
+	dvb_free_config(mpriv->config);
 	menu_list_uninit(menu, free_entry);
 }
 


More information about the MPlayer-dev-eng mailing list