Browse Source

Fix memory leaks.

Holding a static set of IDisposables for direct bindings wasn't a good
idea - it caused leaks. Use a per-instance list.
Steven Kirk 9 năm trước cách đây
mục cha
commit
06d37b85d0
1 tập tin đã thay đổi với 14 bổ sung14 xóa
  1. 14 14
      src/Perspex.Base/PerspexObject.cs

+ 14 - 14
src/Perspex.Base/PerspexObject.cs

@@ -23,17 +23,6 @@ namespace Perspex
     /// </remarks>
     public class PerspexObject : IPerspexObject, IPerspexObjectDebug, INotifyPropertyChanged, IPriorityValueOwner
     {
-        /// <summary>
-        /// Maintains a list of direct property binding subscriptions so that the binding source
-        /// doesn't get collected.
-        /// </summary>
-        /// <remarks>
-        /// If/when we provide a ClearBindings() method, then this collection will be need to be
-        /// moved to an instance field and indexed by property, but until that point a static
-        /// collection will suffice.
-        /// </remarks>
-        private static List<IDisposable> s_directBindings = new List<IDisposable>();
-
         /// <summary>
         /// The parent object that inherited values are inherited from.
         /// </summary>
@@ -45,6 +34,12 @@ namespace Perspex
         private readonly Dictionary<PerspexProperty, PriorityValue> _values =
             new Dictionary<PerspexProperty, PriorityValue>();
 
+        /// <summary>
+        /// Maintains a list of direct property binding subscriptions so that the binding source
+        /// doesn't get collected.
+        /// </summary>
+        private List<IDisposable> _directBindings;
+
         /// <summary>
         /// Event handler for <see cref="INotifyPropertyChanged"/> implementation.
         /// </summary>
@@ -402,17 +397,22 @@ namespace Perspex
 
                 IDisposable subscription = null;
 
+                if (_directBindings == null)
+                {
+                    _directBindings = new List<IDisposable>();
+                }
+
                 subscription = source
                     .Select(x => CastOrDefault(x, property.PropertyType))
-                    .Do(_ => { }, () => s_directBindings.Remove(subscription))
+                    .Do(_ => { }, () => _directBindings.Remove(subscription))
                     .Subscribe(x => DirectBindingSet(property, x));
 
-                s_directBindings.Add(subscription);
+                _directBindings.Add(subscription);
 
                 return Disposable.Create(() =>
                 {
                     subscription.Dispose();
-                    s_directBindings.Remove(subscription);
+                    _directBindings.Remove(subscription);
                 });
             }
             else