Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[controls] fix leak in ImageSource, caused by Task that never complet…
…es (#21928) Context: https://github.com/AdamEssenmacher/MemoryToolkit.Maui/tree/main/samples Testing the above sample, I saw some odd results in a `.gcdump` related to `UriImageSource` inside a `CollectionView`: AsyncTaskMethodBuilder+AsyncStateMachineBox<VoidTaskResult, Microsoft.Maui.Controls.ImageElement+<CancelOldValue>d__10>, Count: 182 Dictionary+Entry<Int32, Task>[], Count: 182 Dictionary<Int32, Task> [Static variable Task.s_currentActiveTasks], Count: 1 It appears that we `await` a `Task` that never completes, which is: https://github.com/dotnet/maui/blob/8e4450cbc14932a6c74aeb8b7bfee9c94eca18b0/src/Controls/src/Core/ImageElement.cs#L129 The `Task` also holds onto the `ImageSource` and whatever memory it happened to use. That would be ok if the `Task` completed. I could reproduce the problem in a test: [Fact] public async Task CancelCompletes() { var imageSource = new StreamImageSource { Stream = _ => Task.FromResult<Stream>(new MemoryStream()) }; await ((IStreamImageSource)imageSource).GetStreamAsync(); await imageSource.Cancel(); // This should complete! } This non-completing `Task` can occur when changing `Image.Source`, which would certainly be bad while scrolling a `CollectionView`! To fix, I refactored the code slightly and had the problematic case return: return Task.FromResult(false);
- Loading branch information