-
Notifications
You must be signed in to change notification settings - Fork 202
[Fix]IDisposable Analyzer Warnings #545
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
weiyuanyue
wants to merge
31
commits into
main
Choose a base branch
from
milly/idisp
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 21 commits
Commits
Show all changes
31 commits
Select commit
Hold shift + click to select a range
9c908a3
fix
9d0f064
fix
bd36d42
fix
a6e25c2
fix
836ffda
fix
07bf8eb
fix
1597cf4
fix
d76bc0c
fix
8bdaf0a
fix
576f3d2
Add UT
5d6bbea
Fix
234531c
format
a284b13
typo
ee76e57
remove
ec0aa03
remove
90a11ff
fix
6db4ec9
Merge branch 'main' into milly/idisp
weiyuanyue 83c66c0
fix
3be0b47
Merge remote-tracking branch 'origin/milly/idisp' into milly/idisp
5b39319
Merge branch 'main' into milly/idisp
weiyuanyue 05d68f8
Fix resource disposal issues
0e2494b
Fix resource disposal issues (#551)
weiyuanyue 6911064
Clarify pragma warning comment for outputBitmap ownership transfer (#…
Copilot 86af1be
Fix copilot comments
f9029e1
Conflict
2d27741
Conflict
d8e161f
Refactor: Replace scattered IDISP004 pragmas with ProcessHelper utili…
33bfad5
conflict
cc0c5e9
Fixed
7aa4541
Merge branch 'main' into milly/idisp
weiyuanyue 209fdc2
Merge branch 'main' into milly/idisp
weiyuanyue File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
114 changes: 114 additions & 0 deletions
114
AIDevGallery.Tests/UnitTests/Samples/ResourceLifecycleTests.cs
This file contains hidden or 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,114 @@ | ||
| // Copyright (c) Microsoft Corporation. All rights reserved. | ||
| // Licensed under the MIT License. | ||
|
|
||
| using Microsoft.VisualStudio.TestTools.UnitTesting; | ||
| using System.Threading.Tasks; | ||
| using Windows.Graphics.Imaging; | ||
|
|
||
| namespace AIDevGallery.Tests.UnitTests.Samples; | ||
|
|
||
| /// <summary> | ||
| /// Tests for BackgroundRemover's SetImageSource method to verify bitmap lifecycle management | ||
| /// </summary> | ||
| [TestClass] | ||
| public class BitmapLifecycleTests | ||
| { | ||
| [TestMethod] | ||
| public void SoftwareBitmapConvertCreatesIndependentCopy() | ||
| { | ||
| // This test verifies that SoftwareBitmap.Convert creates an independent copy | ||
| // and that disposing the original doesn't affect the converted bitmap. | ||
| // This is critical for BackgroundRemover's lifecycle management. | ||
|
|
||
| // Arrange | ||
| var originalBitmap = new SoftwareBitmap( | ||
| BitmapPixelFormat.Rgba8, | ||
| 100, | ||
| 100, | ||
| BitmapAlphaMode.Premultiplied); | ||
|
|
||
| // Act | ||
| var convertedBitmap = SoftwareBitmap.Convert( | ||
| originalBitmap, | ||
| BitmapPixelFormat.Bgra8, | ||
| BitmapAlphaMode.Premultiplied); | ||
|
|
||
| // Assert - Verify it's a new instance with correct properties | ||
| Assert.IsNotNull(convertedBitmap, "Converted bitmap should not be null"); | ||
| Assert.AreNotSame(originalBitmap, convertedBitmap, "Convert should create a new instance"); | ||
| Assert.AreEqual(originalBitmap.PixelWidth, convertedBitmap.PixelWidth, "Width should match"); | ||
| Assert.AreEqual(originalBitmap.PixelHeight, convertedBitmap.PixelHeight, "Height should match"); | ||
| Assert.AreEqual(BitmapPixelFormat.Bgra8, convertedBitmap.BitmapPixelFormat, "Format should be converted"); | ||
|
|
||
| // Dispose original - this should NOT affect the converted bitmap | ||
| originalBitmap.Dispose(); | ||
|
|
||
| // Assert - converted bitmap should still be usable after disposing original | ||
| Assert.AreEqual(100, convertedBitmap.PixelWidth, "Converted bitmap should remain valid"); | ||
| Assert.AreEqual(100, convertedBitmap.PixelHeight, "Converted bitmap should remain valid"); | ||
|
|
||
| // Clean up | ||
| convertedBitmap.Dispose(); | ||
| } | ||
|
|
||
| [TestMethod] | ||
| public async Task BackgroundRemoverBitmapLifecycleVerifyCorrectPattern() | ||
| { | ||
| // *** CRITICAL TEST FOR BackgroundRemover.xaml.cs *** | ||
| // This test validates the exact pattern used in BackgroundRemover: | ||
| // | ||
| // Pattern in BackgroundRemover.xaml.cs: | ||
| // using (var outputBitmap = await ExtractBackground(...)) | ||
| // { | ||
| // await SetImageSource(outputBitmap); // Converts and uses bitmap | ||
| // } // outputBitmap is disposed here | ||
| // | ||
| // Key Question: Is it safe to dispose outputBitmap after SetImageSource? | ||
| // Answer: YES, because SoftwareBitmap.Convert creates an independent copy. | ||
| // | ||
| // This test verifies that the converted bitmap remains valid even after | ||
| // the original outputBitmap is disposed, ensuring no use-after-free bugs. | ||
|
|
||
| // Arrange - Create a bitmap (simulating ExtractBackground result) | ||
| SoftwareBitmap? outputBitmap = null; | ||
| SoftwareBitmap? convertedBitmap = null; | ||
weiyuanyue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| try | ||
| { | ||
| outputBitmap = new SoftwareBitmap( | ||
| BitmapPixelFormat.Rgba8, | ||
| 200, | ||
| 200, | ||
| BitmapAlphaMode.Premultiplied); | ||
|
|
||
| // Act - Simulate SetImageSource logic | ||
| // Inside SetImageSource, this happens: | ||
| convertedBitmap = SoftwareBitmap.Convert( | ||
| outputBitmap, | ||
| BitmapPixelFormat.Bgra8, | ||
| BitmapAlphaMode.Premultiplied); | ||
|
|
||
| // At this point in BackgroundRemover, we would: | ||
| // await bitmapSource.SetBitmapAsync(convertedBitmap); | ||
| // The SetBitmapAsync call completes synchronously with the bitmap data | ||
| Assert.IsNotNull(convertedBitmap, "Converted bitmap should be created"); | ||
| Assert.AreEqual(200, convertedBitmap.PixelWidth, "Initial width should be correct"); | ||
|
|
||
| // Now simulate the using block exiting - dispose outputBitmap | ||
| // This is the CRITICAL moment: does disposing outputBitmap break convertedBitmap? | ||
| outputBitmap.Dispose(); | ||
|
|
||
| // Assert - convertedBitmap should still be valid because it's an independent copy | ||
| Assert.AreEqual(200, convertedBitmap.PixelWidth, "Width should still be accessible"); | ||
| Assert.AreEqual(200, convertedBitmap.PixelHeight, "Height should still be accessible"); | ||
| Assert.AreEqual(BitmapPixelFormat.Bgra8, convertedBitmap.BitmapPixelFormat, "Format should still be accessible"); | ||
|
|
||
| await Task.CompletedTask; // Simulate async operation | ||
| } | ||
| finally | ||
| { | ||
| // Clean up - in real code, the UI framework holds the convertedBitmap reference | ||
| convertedBitmap?.Dispose(); | ||
| } | ||
| } | ||
weiyuanyue marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.