Fixed memory management issues in OpacityMaskView#459
Open
HotCakeX wants to merge 12 commits intomicrosoft:mainfrom
Open
Fixed memory management issues in OpacityMaskView#459HotCakeX wants to merge 12 commits intomicrosoft:mainfrom
HotCakeX wants to merge 12 commits intomicrosoft:mainfrom
Conversation
Event handlers in the HoverLight weren't being unsubscribed On Disconnected.
OpacityMaskView wasn't handling resources properly on unload, causing memory leaks and also changing ```csharp private static CompositionBrush GetVisualBrush(UIElement element) ``` to ```csharp private static CompositionSurfaceBrush GetVisualBrush(UIElement element) ``` improves performance.
This reverts commit 8cde6c8.
This reverts commit 972bd09.
OpacityMaskView wasn't handling resources properly on unload, causing memory leaks and also changing ```csharp private static CompositionBrush GetVisualBrush(UIElement element) ``` to ```csharp private static CompositionSurfaceBrush GetVisualBrush(UIElement element) ``` improves performance.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR addresses memory management issues in the OpacityMaskView control by adding proper disposal of composition resources. The changes introduce cleanup logic when the control unloads and track composition objects for explicit disposal.
Key Changes:
- Added CleanupComposition method and Unloaded event handler to dispose composition resources
- Changed GetVisualBrush return type from CompositionBrush to CompositionSurfaceBrush
- Added fields to track composition resources (_sourceBrush, _rootGrid, _redirectVisual) for proper cleanup
Comments suppressed due to low confidence (2)
AIDevGallery/Controls/OpacityMask.xaml.cs:45
- The XML documentation contains duplicate sentences with slightly different wording. The summary should only contain one clear description. Remove the redundant "Creates a new instance of the OpacityMaskView class." line.
/// Initializes a new instance of the <see cref="OpacityMaskView"/> class.
/// Creates a new instance of the <see cref="OpacityMaskView"/> class.
/// </summary>
AIDevGallery/Controls/OpacityMask.xaml.cs:112
- The GetVisualBrush method creates CompositionVisualSurface and ExpressionAnimation objects that are never disposed, creating a memory leak. These composition resources should be tracked as fields (similar to _sourceBrush) and disposed in CleanupComposition, or the method should return a tuple/wrapper that includes these resources so they can be disposed properly.
private static CompositionSurfaceBrush GetVisualBrush(UIElement element)
{
Visual visual = ElementCompositionPreview.GetElementVisual(element);
Compositor compositor = visual.Compositor;
CompositionVisualSurface visualSurface = compositor.CreateVisualSurface();
visualSurface.SourceVisual = visual;
ExpressionAnimation sourceSizeAnimation = compositor.CreateExpressionAnimation($"{nameof(visual)}.Size");
sourceSizeAnimation.SetReferenceParameter(nameof(visual), visual);
visualSurface.StartAnimation(nameof(visualSurface.SourceSize), sourceSizeAnimation);
CompositionSurfaceBrush brush = compositor.CreateSurfaceBrush(visualSurface);
visual.Opacity = 0;
return brush;
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
OpacityMaskView wasn't handling resources properly on unload, causing memory leaks and also changing
to
improves performance.