-
Notifications
You must be signed in to change notification settings - Fork 0
/
AppDelegate.cs
71 lines (58 loc) · 1.51 KB
/
AppDelegate.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
namespace MemoryLeaksOniOS;
[Register ("AppDelegate")]
public class AppDelegate : UIApplicationDelegate {
public override UIWindow? Window {
get;
set;
}
public override bool FinishedLaunching (UIApplication application, NSDictionary launchOptions)
{
// create a new window instance based on the screen size
Window = new UIWindow (UIScreen.MainScreen.Bounds);
var button = new UIButton(Window.Frame);
button.SetTitle("Click Me", UIControlState.Normal);
button.TouchUpInside += OnClicked;
var vc = new UIViewController ();
vc.View!.AddSubview (button);
Window.RootViewController = vc;
// make the window visible
Window.MakeKeyAndVisible ();
return true;
}
async void OnClicked(object? sender, EventArgs e)
{
WeakReference objReference, viewReference;
{
objReference = new WeakReference(new object());
var parent = new MyViewSubclass();
var view = new MyViewSubclass();
parent.Add(view);
// Uncomment to solve the leak
//view.Parent = null;
viewReference = new WeakReference(view);
}
// 10 GCs to just be thorough
for (int i = 0; i < 10; i++)
{
await Task.Yield();
GC.Collect();
GC.WaitForPendingFinalizers();
}
new UIAlertView("Results",
$"""
new object() is alive: {objReference.IsAlive}
new MyViewSubclass() is alive: {viewReference.IsAlive}
"""
,
null, "Ok").Show();
}
class MyViewSubclass : UIView
{
public UIView? Parent { get; set; }
public void Add(MyViewSubclass subview)
{
subview.Parent = this;
AddSubview(subview);
}
}
}