Memory leak when using callbacks?
This can be just my misunderstanding, but there is something weird going on that causes GStreamer to free its memory correctly (confirmed by tracing subsystem), but leave some of Rust objects in memory forever.
I have a struct that looks like this:
pub struct Stream {
pipeline: Pipeline,
webrtcbin: Element,
}
impl Drop for Stream {
fn drop(&mut self) {
println!("Destroyed!");
}
}
With weak references I stop the pipeline, remove last reference and this struct is dropped. Good so far.
Now consider this struct:
struct Test(u32);
impl Drop for Test {
fn drop(&mut self) {
println!("Drop");
}
}
And its usage:
let test = Test(1);
stream.webrtcbin.connect_notify(Some("connection-state"), {
move |_webrtcbin, _spec| {
println!("{}", test.0);
}
});
Intuitively the only thing holding test
is the callback and when pipeline is stopped and last reference to Stream
struct is removed from Rust point of view (Stream::drop
is called), test
should also be dropped.
Confusingly, it is still kept in memory without any way to remove it. Even more confusing is the fact that Arc
is not involved here.
What is the recommended approach in this case and shouldn't bindings handle this in some way?