From f47b66b315ab38c4e786a8d9e73fb8992faab5ba Mon Sep 17 00:00:00 2001 From: Rabindra Harlalka Date: Wed, 5 Aug 2020 13:06:17 +0530 Subject: [PATCH] Opaque: Drop extra reference when object is un-owned before dispose If the Owned property of a Gst.MiniObject (base class Opaque) is set from true to false during its lifetime, its reference count is not decremented. For example when Gst.Event (derived from Gst.MiniObject) is created using Gst.Event.NewEos(), Gst.MiniObject.Ref() increments its ref count and sets Owned = true. Then, passing the event to Gst.Pad.PushEvent(), sets Owned = false, but this change won't remove the extra reference that MiniObject.Ref() added earlier and it won't get cleared even when Unref() gets eventualy called from Dispose(), because Gst.MiniObject.Unref() only decrements ref count of objects that are Owned. This change Unref()s the object when Owned property gets set to false. --- Source/glib/Opaque.cs | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/Source/glib/Opaque.cs b/Source/glib/Opaque.cs index 955285f6a..155416fb9 100644 --- a/Source/glib/Opaque.cs +++ b/Source/glib/Opaque.cs @@ -32,6 +32,8 @@ public class Opaque : IWrapper, IDisposable { IntPtr _obj; bool owned; + bool disposing; + bool setOwnedRecursionGuard; public static Opaque GetOpaque (IntPtr o, Type type, bool owned) { @@ -42,7 +44,9 @@ public static Opaque GetOpaque (IntPtr o, Type type, bool owned) if (owned) { if (opaque.owned) { // The constructor took a Ref it shouldn't have, so undo it + opaque.setOwnedRecursionGuard = true; opaque.Unref (o); + opaque.setOwnedRecursionGuard = false; } opaque.owned = true; } else @@ -85,6 +89,7 @@ protected IntPtr Raw { public virtual void Dispose () { + disposing = true; Raw = IntPtr.Zero; GC.SuppressFinalize (this); } @@ -119,6 +124,14 @@ public bool Owned { return owned; } set { + // unref if Owned is set to false without disposing this object + if (setOwnedRecursionGuard) return; + if (owned && !value && _obj != IntPtr.Zero && !disposing) { + setOwnedRecursionGuard = true; + this.Unref(_obj); + setOwnedRecursionGuard = false; + } + owned = value; } }