Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Don't return the same view for TableView if not enabling reuse
- Loading branch information
Showing
5 changed files
with
211 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
src/Controls/samples/Controls.Sample.UITests/Issues/Issue5555.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Threading.Tasks; | ||
using Microsoft.Maui.Controls; | ||
|
||
namespace Maui.Controls.Sample.Issues | ||
{ | ||
[Issue(IssueTracker.None, 5555, "Memory leak when SwitchCell or EntryCell", PlatformAffected.iOS)] | ||
public class Issue5555 : TestContentPage | ||
{ | ||
public static Label DestructorCount = new Label() { Text = "0" }; | ||
protected override void Init() | ||
{ | ||
var result = new Label | ||
{ | ||
FontSize = 16, | ||
Text = "Click 'Push page' twice" | ||
}; | ||
|
||
var list = new List<WeakReference>(); | ||
|
||
var checkButton = new Button | ||
{ | ||
Text = "Check Result", | ||
IsEnabled = false, | ||
Command = new Command(async () => | ||
{ | ||
if (list.Count < 2) | ||
{ | ||
result.Text = "Click 'Push page' again"; | ||
return; | ||
} | ||
try | ||
{ | ||
await GarbageCollectionHelper.WaitForGC(2500, list.ToArray()); | ||
result.Text = "Success"; | ||
} | ||
catch (Exception) | ||
{ | ||
result.Text = "Failed"; | ||
return; | ||
} | ||
}) | ||
}; | ||
|
||
Content = new StackLayout | ||
{ | ||
Children = { | ||
DestructorCount, | ||
result, | ||
new Button | ||
{ | ||
Text = "Push page", | ||
Command = new Command(async() => { | ||
if (list.Count >= 2) | ||
list.Clear(); | ||
var wref = new WeakReference(new LeakPage()); | ||
await Navigation.PushAsync(wref.Target as Page); | ||
await (wref.Target as Page).Navigation.PopAsync(); | ||
list.Add(wref); | ||
if (list.Count > 1) | ||
{ | ||
checkButton.IsEnabled = true; | ||
result.Text = "You can check result"; | ||
} | ||
else | ||
{ | ||
result.Text = "Again"; | ||
} | ||
}) | ||
}, | ||
checkButton | ||
} | ||
}; | ||
} | ||
|
||
class LeakPage : ContentPage | ||
{ | ||
public LeakPage() | ||
{ | ||
Content = new StackLayout | ||
{ | ||
Children = { | ||
new Entry { Text = "LeakPage" }, | ||
new TableView | ||
{ | ||
Root = new TableRoot | ||
{ | ||
new TableSection | ||
{ | ||
new SwitchCell { Text = "switch cell", On = true }, | ||
new EntryCell { Text = "entry cell" } | ||
} | ||
} | ||
} | ||
} | ||
}; | ||
} | ||
|
||
~LeakPage() | ||
{ | ||
System.Diagnostics.Debug.WriteLine("LeakPage Finalized"); | ||
} | ||
} | ||
} | ||
} |
63 changes: 63 additions & 0 deletions
63
src/Controls/samples/Controls.Sample.UITests/Utils/GarbageCollectionHelper.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace Maui.Controls.Sample | ||
{ | ||
public static class GarbageCollectionHelper | ||
{ | ||
public static void Collect() | ||
{ | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
} | ||
|
||
public static async Task WaitForGC(params WeakReference[] references) => await WaitForGC(5000, references); | ||
|
||
public static async Task WaitForGC(int timeout, params WeakReference[] references) | ||
{ | ||
bool referencesCollected() | ||
{ | ||
GC.Collect(); | ||
GC.WaitForPendingFinalizers(); | ||
|
||
foreach (var reference in references) | ||
{ | ||
if (reference.IsAlive) | ||
{ | ||
return false; | ||
} | ||
} | ||
|
||
return true; | ||
} | ||
|
||
await AssertEventually(referencesCollected, timeout); | ||
} | ||
|
||
public static async Task AssertEventually(this Func<bool> assertion, int timeout = 1000, int interval = 100, string message = "Assertion timed out") | ||
{ | ||
do | ||
{ | ||
if (assertion()) | ||
{ | ||
return; | ||
} | ||
|
||
await Task.Delay(interval); | ||
timeout -= interval; | ||
|
||
} | ||
while (timeout >= 0); | ||
|
||
if (!assertion()) | ||
{ | ||
throw new Exception(message); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
using NUnit.Framework; | ||
using UITest.Appium; | ||
using UITest.Core; | ||
|
||
namespace Microsoft.Maui.AppiumTests.Issues | ||
{ | ||
public class Issue5555 : _IssuesUITest | ||
{ | ||
public override string Issue => "Memory leak when SwitchCell or EntryCell"; | ||
public Issue5555(TestDevice device) : base(device) | ||
{ | ||
} | ||
|
||
[Test] | ||
public void Issue5555Test() | ||
{ | ||
App.Click("Push page"); | ||
App.WaitForElement("Push page"); | ||
App.Click("Push page"); | ||
App.WaitForElement("Push page"); | ||
|
||
App.WaitForElement("You can check result"); | ||
App.Click("Check Result"); | ||
|
||
App.WaitForElement("Success"); | ||
} | ||
} | ||
} |