Advertisement
Forage

Untitled

Oct 5th, 2011
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Diff 4.82 KB | None | 0 0
  1. diff --git a/gstreamer-sharp/MiniObject.cs b/gstreamer-sharp/MiniObject.cs
  2. index cc04b1c..c479382 100644
  3. --- a/gstreamer-sharp/MiniObject.cs
  4. +++ b/gstreamer-sharp/MiniObject.cs
  5. @@ -33,6 +33,7 @@ namespace Gst {
  6.  
  7.    using System;
  8.    using System.Collections;
  9. +  using System.Collections.Generic;
  10.    using System.ComponentModel;
  11.    using System.Reflection;
  12.    using System.Runtime.InteropServices;
  13. @@ -47,10 +48,10 @@ namespace Gst {
  14.  
  15.      [StructLayout (LayoutKind.Sequential) ]
  16.      struct GstMiniObjectClass {
  17. -      GTypeClass parent;
  18. -      IntPtr copy;
  19. -      IntPtr finalize;
  20. -      IntPtr reserved;
  21. +      public GTypeClass parent;
  22. +      public IntPtr copy;
  23. +      public IntPtr finalize;
  24. +      public IntPtr reserved;
  25.      }
  26.  
  27.      [StructLayout (LayoutKind.Sequential) ]
  28. @@ -68,20 +69,28 @@ namespace Gst {
  29.  
  30.      IntPtr handle;
  31.      bool disposed = false;
  32. -    static Hashtable Objects = new Hashtable();
  33. +    static Dictionary<IntPtr, WeakReference> Objects = new Dictionary<IntPtr, WeakReference>();
  34.  
  35.      ~MiniObject () {
  36. -      Dispose ();
  37. +      if (WarnOnFinalize)
  38. +        Console.Error.WriteLine ("Unexpected finalization of " + GetType() + " instance.  Consider calling Dispose.");
  39. +      
  40. +      Dispose (false);
  41.      }
  42.  
  43.      [DllImport ("libgstreamer-0.10.dll") ]
  44.      static extern void gst_mini_object_unref (IntPtr raw);
  45.  
  46. -    public virtual void Dispose () {
  47. +    public void Dispose () {
  48.        if (disposed)
  49.          return;
  50.  
  51. +      Dispose(true);
  52.        disposed = true;
  53. +      GC.SuppressFinalize (this);
  54. +    }
  55. +      
  56. +   protected virtual void Dispose (bool disposing) {
  57.        lock (typeof (MiniObject)) {
  58.          if (handle != IntPtr.Zero) {
  59.            Objects.Remove (handle);
  60. @@ -93,9 +102,10 @@ namespace Gst {
  61.            }
  62.            handle = IntPtr.Zero;
  63.          }
  64. -      }
  65. -      GC.SuppressFinalize (this);
  66. -    }
  67. +      }     
  68. +   }
  69. +
  70. +   public static bool WarnOnFinalize { get; set; }
  71.  
  72.      [DllImport ("libgstreamer-0.10.dll") ]
  73.      static extern IntPtr gst_mini_object_ref (IntPtr raw);
  74. @@ -106,18 +116,17 @@ namespace Gst {
  75.  
  76.        MiniObject obj = null;
  77.        lock (typeof (MiniObject)) {
  78. -        WeakReference weak_ref = Objects[o] as WeakReference;
  79. -
  80. -        if (weak_ref != null && weak_ref.IsAlive)
  81. -          obj = weak_ref.Target as MiniObject;
  82. -
  83. -        if (obj == null)
  84. -          obj = Objects[o] as MiniObject;
  85. +        WeakReference weak_ref;
  86. +        if (Objects.TryGetValue (o, out weak_ref)) {
  87. +          if (weak_ref != null && weak_ref.IsAlive)
  88. +            obj = weak_ref.Target as MiniObject;
  89. +        }
  90.        }
  91.  
  92.        if (obj != null && obj.handle == o) {
  93.          if (owned_ref)
  94.            gst_mini_object_unref (obj.handle);
  95. +        
  96.          obj.disposed = false;
  97.          return obj;
  98.        }
  99. @@ -128,6 +137,7 @@ namespace Gst {
  100.  
  101.        if (!owned_ref)
  102.          gst_mini_object_ref (obj.Handle);
  103. +      
  104.        Objects [o] = new WeakReference (obj);
  105.        return obj;
  106.      }
  107. @@ -194,7 +204,7 @@ namespace Gst {
  108.  
  109.      }
  110.  
  111. -    private static void InvokeClassInitializers (GType gtype, System.Type t) {
  112. +    private static void InvokeTypeInitializers (GType gtype, System.Type t) {
  113.        object[] parms = {gtype, t};
  114.  
  115.        BindingFlags flags = BindingFlags.Static | BindingFlags.NonPublic;
  116. @@ -297,7 +307,7 @@ namespace Gst {
  117.        Gst.GLib.GType.Register (gtype, t);
  118.  
  119.        ConnectDefaultHandlers (gtype, t);
  120. -      InvokeClassInitializers (gtype, t);
  121. +      InvokeTypeInitializers (gtype, t);
  122.        return gtype;
  123.      }
  124.  
  125. @@ -332,18 +342,23 @@ namespace Gst {
  126.  
  127.      protected virtual void CreateNativeObject () {
  128.        Raw = gst_mini_object_new (LookupGType ().Val);
  129. -      Objects [handle] = this;
  130.      }
  131.  
  132.      protected virtual IntPtr Raw {
  133.        get {
  134.          return handle;
  135.        } set {
  136. +        if (handle == value)
  137. +          return;
  138. +        
  139.          if (handle != IntPtr.Zero)
  140.            Objects.Remove (handle);
  141. +        
  142.          handle = value;
  143. +        
  144.          if (value == IntPtr.Zero)
  145.            return;
  146. +        
  147.          Objects [value] = new WeakReference (this);
  148.        }
  149.      }
  150. @@ -360,27 +375,19 @@ namespace Gst {
  151.      }
  152.  
  153.      protected string TypeName {
  154. -      get {
  155. -        return NativeType.ToString();
  156. -      }
  157. +      get { return NativeType.ToString(); }
  158.      }
  159.  
  160.      internal Gst.GLib.GType NativeType {
  161. -      get {
  162. -        return LookupGType ();
  163. -      }
  164. +      get { return LookupGType (); }
  165.      }
  166.  
  167.      public IntPtr Handle {
  168. -      get {
  169. -        return handle;
  170. -      }
  171. +      get { return handle; }
  172.      }
  173.  
  174.      public IntPtr OwnedHandle {
  175. -      get {
  176. -        return gst_mini_object_ref (handle);
  177. -      }
  178. +      get { return gst_mini_object_ref (handle); }
  179.      }
  180.  
  181.      public override int GetHashCode () {
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement