Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Ensure deferred loaded elements are nulled out when unloaded #17967

Merged
merged 2 commits into from
Aug 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void _component_1_update(global::Microsoft.UI.Xaml.ElementStub sender)
that.Bindings.UpdateResources();
that.Bindings.NotifyXLoad("LoadElement");
}
else
{
_LoadElementSubject.ElementInstance = null;
}
}
}
c1.MaterializationChanged += _component_1_update;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,10 @@ void _component_1_update(global::Microsoft.UI.Xaml.ElementStub sender)
that.Bindings.UpdateResources();
that.Bindings.NotifyXLoad("LoadElement");
}
else
{
_LoadElementSubject.ElementInstance = null;
}
}
}
c1.MaterializationChanged += _component_1_update;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6156,6 +6156,23 @@ private void BuildNameCache(XamlObjectDefinition topLevelControl)
}
}
}

private List<string> FindNamesIn(XamlObjectDefinition xamlObjectDefinition)
{
var list = new List<string>();
foreach (var element in EnumerateSubElements(xamlObjectDefinition))
{
var nameMember = FindMember(element, "Name");

if (nameMember?.Value is string name)
{
list.Add(name);
}
}

return list;
}

/// <summary>
/// Statically finds a element by name, given a xaml element root
/// </summary>
Expand Down Expand Up @@ -6362,6 +6379,15 @@ private IEnumerable<XamlObjectDefinition> EnumerateSubElements(IEnumerable<XamlO
writer.AppendLineIndented($"that.Bindings.NotifyXLoad(\"{xName}\");");
}
}

using (writer.BlockInvariant("else"))
{
var elementNames = FindNamesIn(definition);
foreach (var elementName in elementNames)
{
writer.AppendLineIndented($"_{elementName}Subject.ElementInstance = null;");
}
}
}
}

Expand Down
94 changes: 38 additions & 56 deletions src/Uno.UI.RuntimeTests/Tests/Windows_UI_Xaml/Given_xLoad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -256,10 +256,10 @@ public async Task When_Binding_xLoad_Nested()
Assert.IsNotNull(SUT.tb02);
Assert.IsNotNull(SUT.tb03);
Assert.IsNotNull(SUT.panel01);
await AssertIsNullAsync(() => SUT.panel02);
await AssertIsNullAsync(() => SUT.tb04);
Assert.IsNull(SUT.panel02);
Assert.IsNull(SUT.tb04);
Assert.IsNotNull(SUT.tb05);
await AssertIsNullAsync(() => SUT.tb06);
Assert.IsNull(SUT.tb06);

SUT.TopLevelVisiblity2 = false;

Expand All @@ -268,25 +268,25 @@ public async Task When_Binding_xLoad_Nested()
Assert.IsNotNull(SUT.tb01);
Assert.IsNotNull(SUT.tb02);
// Note: If not null, this usually means that the control is leaking!!!
await AssertIsNullAsync(() => SUT.panel01);
await AssertIsNullAsync(() => SUT.tb03);
await AssertIsNullAsync(() => SUT.panel02);
await AssertIsNullAsync(() => SUT.tb04);
await AssertIsNullAsync(() => SUT.tb06);
await AssertIsNullAsync(() => SUT.panel03);
await AssertIsNullAsync(() => SUT.tb05);
Assert.IsNull(SUT.panel01);
Assert.IsNull(SUT.tb03);
Assert.IsNull(SUT.panel02);
Assert.IsNull(SUT.tb04);
Assert.IsNull(SUT.tb06);
Assert.IsNull(SUT.panel03);
Assert.IsNull(SUT.tb05);

SUT.TopLevelVisiblity1 = false;

await AssertIsNullAsync(() => SUT.tb01);
await AssertIsNullAsync(() => SUT.tb02);
await AssertIsNullAsync(() => SUT.panel01);
await AssertIsNullAsync(() => SUT.tb03);
await AssertIsNullAsync(() => SUT.panel02);
await AssertIsNullAsync(() => SUT.tb04);
await AssertIsNullAsync(() => SUT.panel03);
await AssertIsNullAsync(() => SUT.tb05);
await AssertIsNullAsync(() => SUT.tb06);
Assert.IsNull(SUT.tb01);
Assert.IsNull(SUT.tb02);
Assert.IsNull(SUT.panel01);
Assert.IsNull(SUT.tb03);
Assert.IsNull(SUT.panel02);
Assert.IsNull(SUT.tb04);
Assert.IsNull(SUT.panel03);
Assert.IsNull(SUT.tb05);
Assert.IsNull(SUT.tb06);
}

#if __ANDROID__
Expand Down Expand Up @@ -409,10 +409,10 @@ public async Task When_Binding_xLoad_Nested_With_ElementStub_LoadCount()
Assert.IsNotNull(SUT.tb02);
Assert.IsNotNull(SUT.tb03);
Assert.IsNotNull(SUT.panel01);
await AssertIsNullAsync(() => SUT.panel02);
await AssertIsNullAsync(() => SUT.tb04);
Assert.IsNull(SUT.panel02);
Assert.IsNull(SUT.tb04);
Assert.IsNotNull(SUT.tb05);
await AssertIsNullAsync(() => SUT.tb06);
Assert.IsNull(SUT.tb06);
Assert.IsTrue(panel03Stub.Load);
Assert.AreEqual(1, tb01StubChangedCount);
Assert.AreEqual(1, tb02StubChangedCount);
Expand All @@ -425,14 +425,14 @@ public async Task When_Binding_xLoad_Nested_With_ElementStub_LoadCount()

Assert.IsNotNull(SUT.tb01);
Assert.IsNotNull(SUT.tb02);
await AssertIsNullAsync(() => SUT.panel01);
await AssertIsNullAsync(() => SUT.tb03);
await AssertIsNullAsync(() => SUT.panel02);
await AssertIsNullAsync(() => SUT.tb04);
await AssertIsNullAsync(() => SUT.tb06);
Assert.IsNull(SUT.panel01);
Assert.IsNull(SUT.tb03);
Assert.IsNull(SUT.panel02);
Assert.IsNull(SUT.tb04);
Assert.IsNull(SUT.tb06);
Assert.IsFalse(panel03Stub.Load);
await AssertIsNullAsync(() => SUT.panel03);
await AssertIsNullAsync(() => SUT.tb05);
Assert.IsNull(SUT.panel03);
Assert.IsNull(SUT.tb05);
Assert.AreEqual(1, tb01StubChangedCount);
Assert.AreEqual(1, tb02StubChangedCount);
Assert.AreEqual(2, panel01StubChangedCount);
Expand All @@ -442,39 +442,21 @@ public async Task When_Binding_xLoad_Nested_With_ElementStub_LoadCount()

await Task.Yield();

await AssertIsNullAsync(() => SUT.tb01);
await AssertIsNullAsync(() => SUT.tb02);
await AssertIsNullAsync(() => SUT.panel01);
await AssertIsNullAsync(() => SUT.tb03);
await AssertIsNullAsync(() => SUT.panel02);
await AssertIsNullAsync(() => SUT.tb04);
Assert.IsNull(SUT.tb01);
Assert.IsNull(SUT.tb02);
Assert.IsNull(SUT.panel01);
Assert.IsNull(SUT.tb03);
Assert.IsNull(SUT.panel02);
Assert.IsNull(SUT.tb04);
Assert.IsFalse(panel03Stub.Load);
await AssertIsNullAsync(() => SUT.panel03);
await AssertIsNullAsync(() => SUT.tb05);
await AssertIsNullAsync(() => SUT.tb06);
Assert.IsNull(SUT.panel03);
Assert.IsNull(SUT.tb05);
Assert.IsNull(SUT.tb06);
Assert.AreEqual(2, tb01StubChangedCount);
Assert.AreEqual(2, tb02StubChangedCount);
Assert.AreEqual(2, panel01StubChangedCount);
Assert.AreEqual(2, panel02StubChangedCount);
}

private async Task AssertIsNullAsync<T>(Func<T> getter, TimeSpan? timeout = null)
{
timeout ??= TimeSpan.FromSeconds(10);
var sw = Stopwatch.StartNew();

while (sw.Elapsed < timeout && getter() != null)
{
await Task.Delay(100);

// Wait for the ElementNameSubject and ComponentHolder
// instances to release their references.
GC.Collect(2);
GC.WaitForPendingFinalizers();
}

Assert.IsNull(getter());
}
}
}
#endif
6 changes: 6 additions & 0 deletions src/Uno.UI/UI/Xaml/Data/ElementNameSubject.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,12 @@ public object? ElementInstance
};
set
{
if (value is null)
{
_elementInstanceRef = null;
return;
}

_elementInstanceRef = WeakReferencePool.RentWeakReference(this, value);

var target = _elementInstanceRef?.Target;
Expand Down
Loading