Skip to content

Commit

Permalink
Revert "Merge pull request AvaloniaUI#8189 from AvaloniaUI/fixes/8178…
Browse files Browse the repository at this point in the history
…-elementname-binding-leak"

This reverts commit 20ed002.
  • Loading branch information
danwalmsley committed Jun 1, 2022
1 parent aee14ba commit 68c915e
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 132 deletions.
2 changes: 1 addition & 1 deletion src/Avalonia.Base/Collections/Pooled/PooledList.cs
Original file line number Diff line number Diff line change
Expand Up @@ -587,7 +587,7 @@ public void Clear()
if (size > 0 && _clearOnFree)
{
// Clear the elements so that the gc can reclaim the references.
Array.Clear(_items, 0, size);
Array.Clear(_items, 0, _size);
}
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System;
using System.Runtime.ExceptionServices;
using Avalonia.Utilities;

namespace Avalonia.Data.Core.Plugins
{
Expand Down Expand Up @@ -61,10 +60,11 @@ public bool Match(object obj, string propertyName)
return AvaloniaPropertyRegistry.Instance.FindRegistered(o, propertyName);
}

private class Accessor : PropertyAccessorBase, IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>
private class Accessor : PropertyAccessorBase, IObserver<object?>
{
private readonly WeakReference<AvaloniaObject> _reference;
private readonly AvaloniaProperty _property;
private IDisposable? _subscription;

public Accessor(WeakReference<AvaloniaObject> reference, AvaloniaProperty property)
{
Expand Down Expand Up @@ -95,45 +95,29 @@ public override bool SetValue(object? value, BindingPriority priority)
return false;
}

void IWeakEventSubscriber<AvaloniaPropertyChangedEventArgs>.
OnEvent(object? notifyPropertyChanged, WeakEvent ev, AvaloniaPropertyChangedEventArgs e)
{
if (e.Property == _property)
{
SendCurrentValue();
}
}

protected override void SubscribeCore()
{
SubscribeToChanges();
SendCurrentValue();
_subscription = Instance?.GetObservable(_property).Subscribe(this);
}

protected override void UnsubscribeCore()
{
var instance = Instance;

if (instance != null)
WeakEvents.AvaloniaPropertyChanged.Unsubscribe(instance, this);
_subscription?.Dispose();
_subscription = null;
}

private void SendCurrentValue()
void IObserver<object?>.OnCompleted()
{
try
{
var value = Value;
PublishValue(value);
}
catch { }
}

private void SubscribeToChanges()
void IObserver<object?>.OnError(Exception error)
{
var instance = Instance;
ExceptionDispatchInfo.Capture(error).Throw();
}

if (instance != null)
WeakEvents.AvaloniaPropertyChanged.Subscribe(instance, this);
void IObserver<object?>.OnNext(object? value)
{
PublishValue(value);
}
}
}
Expand Down
15 changes: 1 addition & 14 deletions src/Avalonia.Base/Utilities/WeakEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,10 @@ public static readonly WeakEvent<INotifyPropertyChanged, PropertyChangedEventArg
return () => s.PropertyChanged -= handler;
});


/// <summary>
/// Represents PropertyChanged event from <see cref="AvaloniaObject"/>
/// </summary>
public static readonly WeakEvent<AvaloniaObject, AvaloniaPropertyChangedEventArgs>
AvaloniaPropertyChanged = WeakEvent.Register<AvaloniaObject, AvaloniaPropertyChangedEventArgs>(
(s, h) =>
{
EventHandler<AvaloniaPropertyChangedEventArgs> handler = (_, e) => h(s, e);
s.PropertyChanged += handler;
return () => s.PropertyChanged -= handler;
});

/// <summary>
/// Represents CanExecuteChanged event from <see cref="ICommand"/>
/// </summary>
public static readonly WeakEvent<ICommand, EventArgs> CommandCanExecuteChanged =
WeakEvent.Register<ICommand>((s, h) => s.CanExecuteChanged += h,
(s, h) => s.CanExecuteChanged -= h);
}
}
95 changes: 6 additions & 89 deletions tests/Avalonia.LeakTests/ControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Disposables;
using System.Runtime.Remoting.Contexts;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using Avalonia.Controls.Templates;
using Avalonia.Data;
using Avalonia.Diagnostics;
using Avalonia.Input;
using Avalonia.Layout;
using Avalonia.Media;
using Avalonia.Platform;
using Avalonia.Rendering;
using Avalonia.Styling;
using Avalonia.Threading;
using Avalonia.UnitTests;
using Avalonia.VisualTree;
using JetBrains.dotMemoryUnit;
Expand Down Expand Up @@ -621,96 +620,14 @@ public void ItemsRepeater_Is_Freed()
}
}

[Fact]
public void ElementName_Binding_In_DataTemplate_Is_Freed()
{
using (Start())
{
var items = new ObservableCollection<int>(Enumerable.Range(0, 10));
NameScope ns;
TextBox tb;
ListBox lb;
var window = new Window
{
[NameScope.NameScopeProperty] = ns = new NameScope(),
Width = 100,
Height = 100,
Content = new StackPanel
{
Children =
{
(tb = new TextBox
{
Name = "tb",
Text = "foo",
}),
(lb = new ListBox
{
Items = items,
ItemTemplate = new FuncDataTemplate<int>((_, _) =>
new Canvas
{
Width = 10,
Height = 10,
[!Control.TagProperty] = new Binding
{
ElementName = "tb",
Path = "Text",
NameScope = new WeakReference<INameScope>(ns),
}
})
}),
}
}
};

tb.RegisterInNameScope(ns);

window.Show();
window.LayoutManager.ExecuteInitialLayoutPass();

void AssertInitialItemState()
{
var item0 = (ListBoxItem)lb.ItemContainerGenerator.Containers.First().ContainerControl;
var canvas0 = (Canvas)item0.Presenter.Child;
Assert.Equal("foo", canvas0.Tag);
}

Assert.Equal(10, lb.ItemContainerGenerator.Containers.Count());
AssertInitialItemState();

items.Clear();
window.LayoutManager.ExecuteLayoutPass();

Assert.Empty(lb.ItemContainerGenerator.Containers);

dotMemory.Check(memory =>
Assert.Equal(0, memory.GetObjects(where => where.Type.Is<Canvas>()).ObjectsCount));
}
}

private IDisposable Start()
{
void Cleanup()
{
// KeyboardDevice holds a reference to the focused item.
KeyboardDevice.Instance.SetFocusedElement(null, NavigationMethod.Unspecified, KeyModifiers.None);

// Empty the dispatcher queue.
Dispatcher.UIThread.RunJobs();
}

return new CompositeDisposable
{
Disposable.Create(Cleanup),
UnitTestApplication.Start(TestServices.StyledWindow.With(
focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice(),
inputManager: new InputManager()))
};
return UnitTestApplication.Start(TestServices.StyledWindow.With(
focusManager: new FocusManager(),
keyboardDevice: () => new KeyboardDevice(),
inputManager: new InputManager()));
}


private class Node
{
public string Name { get; set; }
Expand Down

0 comments on commit 68c915e

Please sign in to comment.