decodebin3: Fix memory issues with active selection list
authorEdward Hervey <edward@centricular.com>
Tue, 27 Sep 2022 11:41:37 +0000 (13:41 +0200)
committerEdward Hervey <bilboed@bilboed.com>
Tue, 27 Sep 2022 14:21:25 +0000 (16:21 +0200)
This had a couple of issues:
* The backing strings (from GstStream) could disappear
* The actual list wasn't properly reset/freed when decodebin3 was re-used

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

subprojects/gst-plugins-base/gst/playback/gstdecodebin3.c

index 803bebb..94d64cf 100644 (file)
@@ -662,7 +662,7 @@ gst_decodebin3_dispose (GObject * object)
   if (dbin->decodable_factories)
     g_list_free (dbin->decodable_factories);
   g_list_free_full (dbin->requested_selection, g_free);
-  g_list_free (dbin->active_selection);
+  g_list_free_full (dbin->active_selection, g_free);
   g_list_free (dbin->to_activate);
   g_list_free (dbin->pending_select_streams);
   g_clear_object (&dbin->collection);
@@ -1800,8 +1800,9 @@ get_output_for_slot (MultiQueueSlot * slot)
     output->slot = slot;
     GST_DEBUG ("Linking slot %p to new output %p", slot, output);
     slot->output = output;
+    GST_DEBUG ("Adding '%s' to active_selection", stream_id);
     dbin->active_selection =
-        g_list_append (dbin->active_selection, (gchar *) stream_id);
+        g_list_append (dbin->active_selection, (gchar *) g_strdup (stream_id));
   } else
     GST_DEBUG ("Not creating any output for slot %p", slot);
 
@@ -2641,8 +2642,10 @@ reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
   slot->output = NULL;
   output->slot = NULL;
   /* Remove sid from active selection */
+  GST_DEBUG ("Removing '%s' from active_selection", sid);
   for (tmp = dbin->active_selection; tmp; tmp = tmp->next)
     if (!g_strcmp0 (sid, tmp->data)) {
+      g_free (tmp->data);
       dbin->active_selection = g_list_delete_link (dbin->active_selection, tmp);
       break;
     }
@@ -2660,7 +2663,7 @@ reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
       /* Pass target stream id to requested selection */
       dbin->requested_selection =
           g_list_append (dbin->requested_selection, g_strdup (tmp->data));
-      dbin->to_activate = g_list_remove (dbin->to_activate, tmp->data);
+      dbin->to_activate = g_list_delete_link (dbin->to_activate, tmp);
       break;
     }
   }
@@ -2670,8 +2673,9 @@ reassign_slot (GstDecodebin3 * dbin, MultiQueueSlot * slot)
         target_slot, tsid);
     target_slot->output = output;
     output->slot = target_slot;
+    GST_DEBUG ("Adding '%s' to active_selection", tsid);
     dbin->active_selection =
-        g_list_append (dbin->active_selection, (gchar *) tsid);
+        g_list_append (dbin->active_selection, (gchar *) g_strdup (tsid));
     SELECTION_UNLOCK (dbin);
 
     /* Wakeup the target slot so that it retries to send events/buffers
@@ -3182,6 +3186,8 @@ gst_decodebin3_change_state (GstElement * element, GstStateChange transition)
           dbin->default_mq_min_interleave, NULL);
       dbin->current_mq_min_interleave = dbin->default_mq_min_interleave;
       dbin->upstream_selected = FALSE;
+      g_list_free_full (dbin->active_selection, g_free);
+      dbin->active_selection = NULL;
     }
       break;
     default: