From 37a352dc35f6043551426f810393701501d80fd4 Mon Sep 17 00:00:00 2001 From: Guillaume Desmottes Date: Tue, 25 May 2021 10:52:26 +0200 Subject: [PATCH] examples: ensure pipeline is destroyed when application is shutting down --- examples/src/bin/gtksink.rs | 15 +++++++++++---- examples/src/bin/gtkvideooverlay.rs | 15 +++++++++++---- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/examples/src/bin/gtksink.rs b/examples/src/bin/gtksink.rs index 9e07bf042..92a491c60 100644 --- a/examples/src/bin/gtksink.rs +++ b/examples/src/bin/gtksink.rs @@ -134,12 +134,19 @@ fn create_ui(app: >k::Application) { // Pipeline reference is owned by the closure below, so will be // destroyed once the app is destroyed let timeout_id = RefCell::new(Some(timeout_id)); + let pipeline = RefCell::new(Some(pipeline)); app.connect_shutdown(move |_| { - pipeline - .set_state(gst::State::Null) - .expect("Unable to set the pipeline to the `Null` state"); + // GTK will keep the Application alive for the whole process lifetime. + // Wrapping the pipeline in a RefCell> and removing it from it here + // ensures the pipeline is actually destroyed when shutting down, allowing us + // to use the leaks tracer for example. + if let Some(pipeline) = pipeline.borrow_mut().take() { + pipeline + .set_state(gst::State::Null) + .expect("Unable to set the pipeline to the `Null` state"); + pipeline.bus().unwrap().remove_watch().unwrap(); + } - bus.remove_watch().unwrap(); if let Some(timeout_id) = timeout_id.borrow_mut().take() { glib::source_remove(timeout_id); } diff --git a/examples/src/bin/gtkvideooverlay.rs b/examples/src/bin/gtkvideooverlay.rs index 1c6cb357d..52ffdd51c 100644 --- a/examples/src/bin/gtkvideooverlay.rs +++ b/examples/src/bin/gtkvideooverlay.rs @@ -239,12 +239,19 @@ fn create_ui(app: >k::Application) { // Pipeline reference is owned by the closure below, so will be // destroyed once the app is destroyed let timeout_id = RefCell::new(Some(timeout_id)); + let pipeline = RefCell::new(Some(pipeline)); app.connect_shutdown(move |_| { - pipeline - .set_state(gst::State::Null) - .expect("Unable to set the pipeline to the `Null` state"); + // GTK will keep the Application alive for the whole process lifetime. + // Wrapping the pipeline in a RefCell> and removing it from it here + // ensures the pipeline is actually destroyed when shutting down, allowing us + // to use the leaks tracer for example. + if let Some(pipeline) = pipeline.borrow_mut().take() { + pipeline + .set_state(gst::State::Null) + .expect("Unable to set the pipeline to the `Null` state"); + pipeline.bus().unwrap().remove_watch().unwrap(); + } - bus.remove_watch().unwrap(); if let Some(timeout_id) = timeout_id.borrow_mut().take() { glib::source_remove(timeout_id); }