Skip to content
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

fix: SelectingItemsControl Selection memory Leak #15446

Conversation

workgroupengineering
Copy link
Contributor

What does the pull request do?

Avoid SelectingItemsControl memory leak

What is the current behavior?

When SelectingItemsControl or a derived control is bound to ObservableCollection, if you remove the selected item, the removed item will not be released from memory until there is a new _oldSelectedItem, this is because _oldSelectedItem holds a hard reference. If the collection contains only two items and you remove one, the removed item will never be released from memory.

What is the updated/expected behavior with this PR?

How was the solution implemented (if it's not obvious)?

Instead of a hard reference using pre allocated WeakReference.

Checklist

Breaking changes

Obsoletions / Deprecations

Fixed issues

Part of #15443

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0047499-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@workgroupengineering workgroupengineering marked this pull request as ready for review April 19, 2024 16:49
@maxkatz6 maxkatz6 requested a review from grokys April 20, 2024 00:15
Copy link
Member

@MrJul MrJul left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Looks good overall, but there's a slight logic change that should be fixed.

@@ -348,11 +348,11 @@ protected ISelectionModel Selection
}

InitializeSelectionModel(_selection);

if (_oldSelectedItems != SelectedItems)
var seletcedItems = SelectedItems;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: typo, seletcedItemsselectedItems


if (_oldSelectedItems != SelectedItems)
var seletcedItems = SelectedItems;
if (_oldSelectedItems.TryGetTarget(out var oldSelectedItems) || oldSelectedItems != seletcedItems)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't equivalent to the comparison before: it will now always be true as long as we have an oldSelectedItems.

Suggested change
if (_oldSelectedItems.TryGetTarget(out var oldSelectedItems) || oldSelectedItems != seletcedItems)
_oldSelectedItems.TryGetTarget(out var oldSelectedItems);
if (oldSelectedItems != selectedItems)

@avaloniaui-bot
Copy link

You can test this PR using the following package version. 11.2.999-cibuild0047638-alpha. (feed url: https://nuget-feed-all.avaloniaui.net/v3/index.json) [PRBUILDID]

@maxkatz6 maxkatz6 merged commit c575f03 into AvaloniaUI:master Apr 25, 2024
10 checks passed
@maxkatz6 maxkatz6 added the backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch label Apr 25, 2024
@workgroupengineering workgroupengineering deleted the fixes/Controls/SelectingItemsControl/Selection_Leak branch April 25, 2024 03:12
maxkatz6 pushed a commit that referenced this pull request Apr 25, 2024
* fix: SelectingItemsControl

* fix: Address review
@maxkatz6 maxkatz6 added backported-11.1.x and removed backport-candidate-11.1.x Consider this PR for backporting to 11.1 branch labels Apr 25, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants