hlsdemux2: Fix memory leaks
authorJan Schmidt <jan@centricular.com>
Thu, 23 Jun 2022 16:57:03 +0000 (02:57 +1000)
committerGStreamer Marge Bot <gitlab-merge-bot@gstreamer-foundation.org>
Wed, 29 Jun 2022 14:33:30 +0000 (14:33 +0000)
Clean up various memory leaks

Part-of: <https://gitlab.freedesktop.org/gstreamer/gstreamer/-/merge_requests/2676>

subprojects/gst-plugins-good/ext/adaptivedemux2/hls/gsthlsdemux.c
subprojects/gst-plugins-good/ext/adaptivedemux2/hls/m3u8.c

index c309663..891bade 100644 (file)
@@ -683,6 +683,9 @@ gst_hls_demux_stream_update_tracks (GstAdaptiveDemux * demux,
     gst_adaptive_demux_track_unref (track);
   }
 
+  if (variant_caps)
+    gst_caps_unref (variant_caps);
+
   /* Update the stream object with rendition types.
    * FIXME: rendition_type could be removed */
   stream->stream_type = hlsdemux_stream->rendition_type;
@@ -814,6 +817,8 @@ gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
     /* Is this rendition active in the current variant ? */
     if (!g_strcmp0 (playlist->media_groups[media->mtype], media->group_id)) {
       GST_DEBUG_OBJECT (demux, "Enabling rendition");
+      if (media_stream->current_rendition)
+        gst_hls_rendition_stream_unref (media_stream->current_rendition);
       media_stream->current_rendition = gst_hls_rendition_stream_ref (media);
     }
 
@@ -821,6 +826,10 @@ gst_hls_demux_setup_streams (GstAdaptiveDemux * demux)
       streams = g_list_append (streams, media_stream);
   }
 
+  /* Free the list (but not the contents, which are stored
+   * elsewhere */
+  if (streams)
+    g_list_free (streams);
 
   create_main_variant_stream (hlsdemux);
 
@@ -873,6 +882,10 @@ gst_hls_demux_process_manifest (GstAdaptiveDemux * demux, GstBuffer * buf)
     return FALSE;
   }
 
+  if (hlsdemux->master) {
+    gst_hls_master_playlist_unref (hlsdemux->master);
+    hlsdemux->master = NULL;
+  }
   hlsdemux->master = gst_hls_master_playlist_new_from_data (playlist,
       gst_adaptive_demux_get_manifest_ref_uri (demux));
 
@@ -1656,6 +1669,9 @@ gst_hls_demux_stream_finalize (GObject * object)
   if (hls_stream == hlsdemux->main_stream)
     hlsdemux->main_stream = NULL;
 
+  g_free (hls_stream->lang);
+  g_free (hls_stream->name);
+
   if (hls_stream->playlist) {
     gst_hls_media_playlist_unref (hls_stream->playlist);
     hls_stream->playlist = NULL;
@@ -1668,6 +1684,9 @@ gst_hls_demux_stream_finalize (GObject * object)
   gst_buffer_replace (&hls_stream->pending_typefind_buffer, NULL);
   gst_buffer_replace (&hls_stream->pending_segment_data, NULL);
 
+  if (hls_stream->moov)
+    gst_isoff_moov_box_free (hls_stream->moov);
+
   if (hls_stream->current_key) {
     g_free (hls_stream->current_key);
     hls_stream->current_key = NULL;
@@ -2167,6 +2186,7 @@ gst_hls_demux_update_fragment_info (GstAdaptiveDemux2Stream * stream)
 
   if (GST_ADAPTIVE_DEMUX2_STREAM_NEED_HEADER (stream) && file->init_file) {
     GstM3U8InitFile *header_file = file->init_file;
+    g_free (stream->fragment.header_uri);
     stream->fragment.header_uri = g_strdup (header_file->uri);
     stream->fragment.header_range_start = header_file->offset;
     if (header_file->size != -1) {
index 16f4efb..1edd690 100644 (file)
@@ -1467,6 +1467,8 @@ gst_hls_rendition_stream_unref (GstHLSRenditionStream * media)
 {
   g_assert (media != NULL && media->ref_count > 0);
   if (g_atomic_int_dec_and_test (&media->ref_count)) {
+    if (media->caps)
+      gst_caps_unref (media->caps);
     g_free (media->group_id);
     g_free (media->name);
     g_free (media->uri);
@@ -1795,6 +1797,8 @@ void
 hls_master_playlist_unref (GstHLSMasterPlaylist * playlist)
 {
   if (g_atomic_int_dec_and_test (&playlist->refcount)) {
+    g_list_free_full (playlist->renditions,
+        (GDestroyNotify) gst_hls_rendition_stream_unref);
     g_list_free_full (playlist->variants,
         (GDestroyNotify) gst_hls_variant_stream_unref);
     g_list_free_full (playlist->iframe_variants,
@@ -2134,13 +2138,14 @@ hls_master_playlist_new_from_data (gchar * data, const gchar * base_uri)
                     GST_PTR_FORMAT, media->caps);
               } else {
                 GST_DEBUG ("  Assigning caps %" GST_PTR_FORMAT, media_caps);
-                media->caps = gst_caps_ref (media_caps);
+                gst_caps_replace (&media->caps, media_caps);
               }
             }
           }
           if (!alt_in_variant) {
             GstCaps *new_caps = gst_caps_subtract (stream->caps, media_caps);
             gst_caps_replace (&stream->caps, new_caps);
+            gst_caps_unref (new_caps);
           }
           gst_caps_unref (media_caps);
         }
@@ -2274,7 +2279,9 @@ hls_master_playlist_get_common_caps (GstHLSMasterPlaylist * playlist)
     if (!res) {
       res = gst_caps_copy (stream->caps);
     } else {
-      res = gst_caps_merge_common (res, stream->caps);
+      GstCaps *common_caps = gst_caps_merge_common (res, stream->caps);
+      gst_caps_unref (res);
+      res = common_caps;
       if (!res)
         goto beach;
     }