Ximagesink segfault on element.name() (custom rust tracer)

Summary

When using ximagesink in a pipeline, element.name().as_str() panics on its child GstVideoSink.

Minimal Reproduction Steps

Impl a custom tracer that registers element_post_message_pre and prints the element name that posted the message

use gst::glib;
use gst::prelude::*;
use gst::subclass::prelude::*;
use once_cell::sync::Lazy;

glib::wrapper! {
    pub struct TestTracer(ObjectSubclass<TestTracerImp>) @extends gst::Tracer, gst::Object;
}

pub fn register(plugin: &gst::Plugin) -> Result<(), glib::BoolError> {
    gst::Tracer::register(Some(plugin), "test-tracer", TestTracer::static_type())
}

static CAT: Lazy<gst::DebugCategory> =
    Lazy::new(|| gst::DebugCategory::new("test-tracer", gst::DebugColorFlags::empty(), Some("")));

#[derive(Default)]
pub struct TestTracerImp {}

#[glib::object_subclass]
impl ObjectSubclass for TestTracerImp {
    const NAME: &'static str = "GstTestTracer";
    type Type = TestTracer;
    type ParentType = gst::Tracer;
}

impl ObjectImpl for TestTracerImp {
    fn constructed(&self) {
        self.parent_constructed();
        self.register_hook(TracerHook::ElementPostMessagePre);
    }
}

impl GstObjectImpl for TestTracerImp {}

impl TracerImpl for TestTracerImp {
    fn element_post_message_pre(&self, _ts: u64, element: &gst::Element, message: &gst::Message) {
        println!("element class name = {}", element.class().type_().name());
        println!("element name = {}", element.name().as_str()); // panics here
    }
}

compile and run:

RUST_BACKTRACE=full GST_TRACERS=test-tracer gst-launch-1.0 -v videotestsrc ! queue ! ximagesink

You will notice that println!("element class name = {}", element.class().type_().name()); will print: element class name = GstVideoSink. And then it will panic on this line: println!("element name = {}", element.name().as_str());

stdout and backtrace:

element class name = GstVideoSink
thread '<unnamed>' panicked at /home/jaydenelliott/.cargo/git/checkouts/gtk-rs-core-7be42ca38bd6361c/8922643/glib/src/gstring.rs:1917:9:
assertion failed: !ptr.is_null()
stack backtrace:
   0:     0x7ae2268042e6 - std::backtrace_rs::backtrace::libunwind::trace::hbee8a7973eeb6c93
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/libunwind.rs:104:5
   1:     0x7ae2268042e6 - std::backtrace_rs::backtrace::trace_unsynchronized::hc8ac75eea3aa6899
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/../../backtrace/src/backtrace/mod.rs:66:5
   2:     0x7ae2268042e6 - std::sys_common::backtrace::_print_fmt::hc7f3e3b5298b1083
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:68:5
   3:     0x7ae2268042e6 - <std::sys_common::backtrace::_print::DisplayBacktrace as core::fmt::Display>::fmt::hbb235daedd7c6190
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:44:22
   4:     0x7ae226822e50 - core::fmt::rt::Argument::fmt::h76c38a80d925a410
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/rt.rs:142:9
   5:     0x7ae226822e50 - core::fmt::write::h3ed6aeaa977c8e45
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/fmt/mod.rs:1120:17
   6:     0x7ae22680274f - std::io::Write::write_fmt::h78b18af5775fedb5
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/io/mod.rs:1810:15
   7:     0x7ae2268040c4 - std::sys_common::backtrace::_print::h5d645a07e0fcfdbb
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:47:5
   8:     0x7ae2268040c4 - std::sys_common::backtrace::print::h85035a511aafe7a8
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:34:9
   9:     0x7ae226805437 - std::panicking::default_hook::{{closure}}::hcce8cea212785a25
  10:     0x7ae226805199 - std::panicking::default_hook::hf5fcb0f213fe709a
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:292:9
  11:     0x7ae2268058c8 - std::panicking::rust_panic_with_hook::h095fccf1dc9379ee
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:779:13
  12:     0x7ae226805769 - std::panicking::begin_panic_handler::{{closure}}::h032ba12139b353db
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:649:13
  13:     0x7ae2268047e6 - std::sys_common::backtrace::__rust_end_short_backtrace::h9259bc2ff8fd0f76
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/sys_common/backtrace.rs:171:18
  14:     0x7ae226805500 - rust_begin_unwind
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/std/src/panicking.rs:645:5
  15:     0x7ae2267c3e95 - core::panicking::panic_fmt::h784f20a50eaab275
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:72:14
  16:     0x7ae2267c3f53 - core::panicking::panic::hb837a5ebbbe5b188
                               at /rustc/07dca489ac2d933c78d3c5158e3f43beefeb02ce/library/core/src/panicking.rs:144:5
  17:     0x7ae2267dcba5 - <glib::gstring::GString as glib::translate::FromGlibPtrFull<*mut u8>>::from_glib_full::h1b8b04581948e288
                               at /home/jaydenelliott/.cargo/git/checkouts/gtk-rs-core-7be42ca38bd6361c/8922643/glib/src/gstring.rs:1917:9
  18:     0x7ae2267dabb6 - glib::translate::from_glib_full::ha7644b08b41f2461
                               at /home/jaydenelliott/.cargo/git/checkouts/gtk-rs-core-7be42ca38bd6361c/8922643/glib/src/translate.rs:1634:5
  19:     0x7ae2267dcc57 - <glib::gstring::GString as glib::translate::FromGlibPtrFull<*mut i8>>::from_glib_full::he05ade9901dd6ad3
                               at /home/jaydenelliott/.cargo/git/checkouts/gtk-rs-core-7be42ca38bd6361c/8922643/glib/src/gstring.rs:1933:9
  20:     0x7ae2267dabd6 - glib::translate::from_glib_full::hb4d083adabcca2cc
                               at /home/jaydenelliott/.cargo/git/checkouts/gtk-rs-core-7be42ca38bd6361c/8922643/glib/src/translate.rs:1634:5
  21:     0x7ae2267c4ab4 - gstreamer::auto::object::GstObjectExt::name::hdd347312df1b3e93
                               at /home/jaydenelliott/.cargo/git/checkouts/gstreamer-rs-79e52a2d27eb91a3/7433ea7/gstreamer/src/auto/object.rs:115:18
  22:     0x7ae2267cc2ca - <gstrstutorial::test_tracer::TestTracerImp as gstreamer::subclass::tracer::TracerImpl>::element_post_message_pre::hfffa73505a7ce453
                               at /home/jaydenelliott/Repos/temp-rs/src/test_tracer.rs:39:39
  23:     0x7ae2267c7719 - <T as gstreamer::subclass::tracer::TracerImplExt>::register_hook::callback::h56ab9e5e023f5a41
                               at /home/jaydenelliott/.cargo/git/checkouts/gstreamer-rs-79e52a2d27eb91a3/7433ea7/gstreamer/src/subclass/tracer.rs:184:9
  24:     0x7ae226fcf823 - gst_element_post_message
  25:     0x7ae22617a8de - <unknown>
  26:     0x7ae226e0405b - g_type_create_instance
  27:     0x7ae226de9b11 - <unknown>
  28:     0x7ae226deb0c7 - g_object_new_with_properties
  29:     0x7ae226fda2d5 - gst_element_factory_create_with_properties
  30:     0x7ae226fa2332 - <unknown>
  31:     0x7ae227054f48 - gst_parse_launch_full
  32:     0x7ae22705614b - gst_parse_launchv_full
  33:     0x625de4b58705 - <unknown>
  34:     0x7ae226c0acd0 - <unknown>
  35:     0x7ae226c0ad8a - __libc_start_main
  36:     0x625de4b56055 - <unknown>
  37:                0x0 - <unknown>
fatal runtime error: failed to initiate panic, error 5
[1]    347064 IOT instruction (core dumped)  RUST_BACKTRACE=full GST_TRACERS=test-tracer gst-launch-1.0 -v videotestsrc ! 

Setup

gst-launch-1.0 --version                           
gst-launch-1.0 version 1.24.1
GStreamer 1.24.1
https://www.archlinux.org/

Any help would be appreciated

When your TestTracer processes the first message, the element which posted this message is still being initialized and its name is not assigned yet. The gst::Object::name() accessor builds a GString assuming the underlying C string is not NULL, which is acceptable in most cases. As a workaround, you could use:

    println!(
        "element name {:?}",
        element.property::<Option<glib::GString>>("name")
    );
1 Like

It sounds like you’re running into this bug: Avoid setting state, posting message or sending query when constructing objects (#2794) · Issues · GStreamer / gstreamer · GitLab

Since you’re using GStreamer 1.24, you could also check if the GstObject CONSTRUCTED flag is set.

1 Like