@@ -28,13 +28,19 @@ public async Task ButtonWithCommandBindingCanBeGarbageCollected()
28
28
buttonWeakRef = new WeakReference ( button ) ;
29
29
proxyWeakRef = new WeakReference ( ( ( ICommandElement ) button ) . CleanupTracker ) ;
30
30
proxyProxyWeakRef = new WeakReference ( ( ( ICommandElement ) button ) . CleanupTracker ? . Proxy ) ;
31
+
32
+ await TestHelpers . Collect ( ) ;
31
33
await TestHelpers . Collect ( ) ;
32
34
33
35
// Make sure everything is still alive if the button is still in scope
36
+ // We need to reference the button here again to keep it alive
37
+ // awaiting a Task appears to move us to a new scope and causes the button to be collected
38
+ Assert . NotNull ( button ) ;
34
39
Assert . True ( buttonWeakRef . IsAlive ) ;
35
40
Assert . True ( proxyWeakRef . IsAlive ) ;
36
41
Assert . True ( proxyProxyWeakRef . IsAlive ) ;
37
- }
42
+ }
43
+
38
44
39
45
Assert . False ( await buttonWeakRef . WaitForCollect ( ) ) ;
40
46
Assert . False ( await proxyWeakRef . WaitForCollect ( ) ) ;
@@ -71,8 +77,12 @@ public async Task ButtonWithWeakEventHandlerIsntCollectedTooEarly()
71
77
proxyWeakRef = new WeakReference ( ( ( ICommandElement ) button ) . CleanupTracker ) ;
72
78
proxyProxyWeakRef = new WeakReference ( ( ( ICommandElement ) button ) . CleanupTracker ? . Proxy ) ;
73
79
await TestHelpers . Collect ( ) ;
80
+ await TestHelpers . Collect ( ) ;
74
81
75
82
// Make sure everything is still alive if the button is still in scope
83
+ // We need to reference the button here again to keep it alive
84
+ // awaiting a Task appears to move us to a new scope and causes the button to be collected
85
+ Assert . NotNull ( button ) ;
76
86
Assert . True ( buttonWeakRef . IsAlive ) ;
77
87
Assert . True ( proxyWeakRef . IsAlive ) ;
78
88
Assert . True ( proxyProxyWeakRef . IsAlive ) ;
0 commit comments