Memory Model of gst::Element in Rust

Hi! I have a quick question about gstreamer-rs bindings.

I’m trying to wrap my mind around how gstreamer Elements are memory-managed in the rust api. I’ve used the C native interface and Python bindings before, where each element was referenced counted either automatically or manually.

How about in rust? When I create an element, do I get ownership of that element? Or do I get ownership of a “Rc”? I’d assume the former, but many places in the examples (for example linking many elements in a pipeline) expect only a reference and the following works: · GitHub

Clearly the src​ and sink​ elements were dropped, and only references to them were used, meaning something was copied out of them for the pipeline to work (side note, the first function calls init and creates a mainloop)

Now, I’d like to have multiple ownership of an element. Do I need Rc? The past example leads me to believe I can just clone()​ it, but I’m not quite sure. I don’t think this would matter for “regular” elements, but I’m playing with AppSink and AppSrc where state matters at all time, not just the declarative state when the element is out of the factory.

Every GObject is behaving conceptually like an Arc<Mutex<State>> (or non-threadsafe ones as Rc<RefCell<State>>). So cloning/dropping them gives you reference counting, and its internal state is handled via interior mutability.

You never need to wrap a plain GObject in an Arc as it already provides the reference counting functionality. You will only need that for more complex structs that might contain some GObject (or multiple) and that needs reference counting over the whole struct.

Note that this is how GObjects are implemented in C (and any other language), this is not something the Rust bindings are adding.

From a memory representation point of view, a gst::Element (or any other GObject) is literally just a GstElement* in C terms, and (after compiler optimizations) almost every function provided by the bindings is literally just a call to the C function.

I see. I’ll clone away then! Thank you very much!