Skip to content

Commit a0ab9d8

Browse files
authored
Merge pull request #864 from bluetarpmedia/Fix515
Fix for #515
2 parents 706f7ac + 9e90b7d commit a0ab9d8

1 file changed

Lines changed: 8 additions & 1 deletion

File tree

test/Microsoft.VisualStudio.Threading.Tests/JoinableTaskTests.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4339,7 +4339,14 @@ private WeakReference<object> SwitchToMainThreadShouldNotLeakJoinableTaskWhenGet
43394339
// the last reference to the JoinableTask.
43404340
this.PushFrameTillQueueIsEmpty();
43414341

4342-
result = null; // TODO: THIS is modifying the closure that we allege to be testing. We should remove this line and get the test to pass.
4342+
// Clear the callback property or otherwise `result` will remain alive (causing `weakResult` to still have a target).
4343+
// This behaviour happens because there are multiple closures in this method. When more than one closure is defined
4344+
// in a method, the C# compiler generates a single private class with multiple methods, one for each closure, and
4345+
// creates fields for all the captured variables from all closures.
4346+
// So although the callback closure only explicitly uses `transitionedToMainThread` the compiler-generated private
4347+
// class also keeps `result` alive.
4348+
factory.PostToUnderlyingSynchronizationContextCallback = null;
4349+
43434350
return weakResult;
43444351
}
43454352

0 commit comments

Comments
 (0)