From 00416a991ce5e24ae622bff4eb4a3054c5aa77e3 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Fri, 8 Apr 2022 15:33:00 +0200 Subject: uriplaylistbin: break reference cycle Passing ownership of item to the probe callback was introducing a reference cycle as the item is owning the sinkpad. --- utils/uriplaylistbin/src/uriplaylistbin/imp.rs | 18 +++++++++++++++--- 1 file changed, 15 insertions(+), 3 deletions(-) diff --git a/utils/uriplaylistbin/src/uriplaylistbin/imp.rs b/utils/uriplaylistbin/src/uriplaylistbin/imp.rs index 92f0e85df..803b1386c 100644 --- a/utils/uriplaylistbin/src/uriplaylistbin/imp.rs +++ b/utils/uriplaylistbin/src/uriplaylistbin/imp.rs @@ -6,8 +6,8 @@ // // SPDX-License-Identifier: MPL-2.0 -use std::sync::Arc; use std::sync::{mpsc, Mutex, MutexGuard}; +use std::sync::{Arc, Weak}; use gst::glib; use gst::prelude::*; @@ -621,6 +621,14 @@ impl Item { _ => panic!("invalid state: {:?}", inner.state), } } + + fn downgrade(&self) -> Weak> { + Arc::downgrade(&self.inner) + } + + fn upgrade(weak: &Weak>) -> Option { + weak.upgrade().map(|inner| Item { inner }) + } } #[derive(Debug, Clone)] @@ -1415,7 +1423,8 @@ impl UriPlaylistBin { // block pad until next item is reaching the `Blocked` state let receiver = Mutex::new(item.receiver()); let element_weak = element.downgrade(); - let item_clone = item.clone(); + // passing ownership of item to the probe callback would introduce a reference cycle as the item is owning the sinkpad + let item_weak = item.downgrade(); sink_pad.add_probe(gst::PadProbeType::BLOCK_DOWNSTREAM, move |pad, info| { let element = match element_weak.upgrade() { @@ -1423,7 +1432,10 @@ impl UriPlaylistBin { None => return gst::PadProbeReturn::Remove, }; let parent = pad.parent().unwrap(); - let item = &item_clone; + let item = match Item::upgrade(&item_weak) { + Some(item) => item, + None => return gst::PadProbeReturn::Remove, + }; if !item.is_streaming() { // block pad until next item is ready -- cgit v1.2.3