[vlc-devel] [PATCH 1/3] Smooth Streaming: fix some memory leaks

Frédéric Yhuel yhuelf at gmail.com
Wed Oct 10 18:54:57 CEST 2012


---
 modules/stream_filter/smooth/smooth.c |   30 ++++++++++++----------------
 modules/stream_filter/smooth/smooth.h |    1 +
 modules/stream_filter/smooth/utils.c  |   35 ++++++++++++++++++++++++++-------
 3 files changed, 42 insertions(+), 24 deletions(-)

diff --git a/modules/stream_filter/smooth/smooth.c b/modules/stream_filter/smooth/smooth.c
index 3e7d9a5..9a4ad20 100644
--- a/modules/stream_filter/smooth/smooth.c
+++ b/modules/stream_filter/smooth/smooth.c
@@ -146,9 +146,7 @@ static int parse_Manifest( stream_t *s )
     }
 
     const char *node;
-    char *stream_name = NULL;
     uint8_t *WaveFormatEx;
-    int stream_type = UNKNOWN_ES;
     sms_stream_t *sms = NULL;
     quality_level_t *ql = NULL;
     int64_t start_time = 0, duration = 0;
@@ -192,15 +190,15 @@ static int parse_Manifest( stream_t *s )
                         if( !strcmp( name, "Type" ) )
                         {
                             if( !strcmp( value, "video" ) )
-                                stream_type = VIDEO_ES;
+                                sms->type = VIDEO_ES;
                             else if( !strcmp( value, "audio" ) )
-                                stream_type = AUDIO_ES;
+                                sms->type = AUDIO_ES;
                             else if( !strcmp( value, "text" ) )
-                                stream_type = SPU_ES;
+                                sms->type = SPU_ES;
                         }
 
                         if( !strcmp( name, "Name" ) )
-                            stream_name = strdup( value );
+                            sms->name = strdup( value );
                         if( !strcmp( name, "TimeScale" ) )
                             sms->timescale = strtoull( value, NULL, 10 );
                         if( !strcmp( name, "FourCC" ) )
@@ -222,18 +220,16 @@ static int parse_Manifest( stream_t *s )
 
                     if( sms && !sms->timescale )
                         sms->timescale = TIMESCALE;
-                    if( !stream_name )
+                    if( !sms->name )
                     {
-                        if( stream_type == VIDEO_ES )
-                            stream_name = strdup( "video" );
-                        else if( stream_type == AUDIO_ES )
-                            stream_name = strdup( "audio" );
-                        else if( stream_type == SPU_ES )
-                            stream_name = strdup( "text" );
+                        if( sms->type == VIDEO_ES )
+                            sms->name = strdup( "video" );
+                        else if( sms->type == AUDIO_ES )
+                            sms->name = strdup( "audio" );
+                        else if( sms->type == SPU_ES )
+                            sms->name = strdup( "text" );
                     }
 
-                    sms->name = stream_name;
-                    sms->type = stream_type;
                     vlc_array_append( p_sys->sms_streams, sms );
                 }
 
@@ -346,8 +342,6 @@ static int parse_Manifest( stream_t *s )
                 if( strcmp( node, "StreamIndex" ) )
                     break;
 
-                stream_name = NULL;
-                stream_type = UNKNOWN_ES;
                 computed_start_time = 0;
                 computed_duration = 0;
                 loop_count = 0;
@@ -502,7 +496,9 @@ static void Close( vlc_object_t *p_this )
             sms_Free( sms );
     }
 
+    sms_queue_free( p_sys->bws );
     vlc_array_destroy( p_sys->sms_streams );
+    vlc_array_destroy( p_sys->selected_st );
     vlc_array_destroy( p_sys->download.chunks );
 
     free( p_sys->base_url );
diff --git a/modules/stream_filter/smooth/smooth.h b/modules/stream_filter/smooth/smooth.h
index dbc32ed..965fa0e 100644
--- a/modules/stream_filter/smooth/smooth.h
+++ b/modules/stream_filter/smooth/smooth.h
@@ -165,6 +165,7 @@ struct stream_sys_t
 #define NO_MORE_CHUNKS ( !p_sys->b_live && \
     no_more_chunks( p_sys->download.ck_index, p_sys->selected_st ) )
 
+void sms_queue_free( sms_queue_t* );
 sms_queue_t *sms_queue_init( const int );
 int sms_queue_put( sms_queue_t *, const uint64_t );
 uint64_t sms_queue_avg( sms_queue_t *);
diff --git a/modules/stream_filter/smooth/utils.c b/modules/stream_filter/smooth/utils.c
index be27c03..d02d63c 100644
--- a/modules/stream_filter/smooth/utils.c
+++ b/modules/stream_filter/smooth/utils.c
@@ -136,6 +136,8 @@ void sms_Free( sms_stream_t *sms )
         vlc_array_destroy( sms->chunks );
     }
 
+    free( sms->name );
+    free( sms->url_template );
     free( sms );
     sms = NULL;
 }
@@ -162,18 +164,37 @@ sms_queue_t *sms_queue_init( const int length )
     return ret;
 }
 
+void sms_queue_free( sms_queue_t* queue )
+{
+    item_t *item = queue->first, *next = NULL;
+    while( item )
+    {
+        next = item->next;
+        FREENULL( item );
+        item = next;
+    }
+    FREENULL( queue );
+}
+
 int sms_queue_put( sms_queue_t *queue, const uint64_t value )
 {
-    item_t *last = queue->first;
-    int i = 0;
-    for( i = 0; i < queue->length - 1; i++ )
+    /* Remove the last (and oldest) item */
+    item_t *item, *prev;
+    int count = 0;
+    for( item = queue->first; item != NULL; item = item->next )
     {
-        if( last )
-            last = last->next;
+        count++;
+        if( count == queue->length )
+        {
+            FREENULL( item );
+            if( prev ) prev->next = NULL;
+            break;
+        }
+        else
+            prev = item;
     }
-    if( i == queue->length - 1 )
-        FREENULL( last );
 
+    /* Now insert the new item */
     item_t *new = malloc( sizeof( item_t ) );
     if( unlikely( !new ) )
         return VLC_ENOMEM;
-- 
1.7.9.5



More information about the vlc-devel mailing list