Skip to content

WeakEventListener is not weak #3029

@dotMorten

Description

@dotMorten

Describe the bug

The current implementation of the weak event listener isn't actually weak. That's because the DetachAction actually captures the object.
For example incc is captured here:
https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/f09b6d4d5dd26b256992d1a19b98c20a5b357181/Microsoft.Toolkit.Uwp.UI.Controls/RotatorTile/RotatorTile.cs#L445

To fix it however, you'd have to change the signature of the DetachAction to not just send the listener but also the instance, which would be a breaking (however necessary) change.

Ie change:
public Action<WeakEventListener<TInstance, TSource, TEventArgs>> OnDetachAction { get; set; }
to
public Action<TInstance, WeakEventListener<TInstance, TSource, TEventArgs>> OnDetachAction { get; set; }

And change the Detach function to:

        public void Detach()
        {
            TInstance target = (TInstance)weakInstance.Target;
            OnDetachAction?.Invoke(target, this);
            OnDetachAction = null;
        }

This would break all uses of the WeakEventListener. The class however is marked non-browsable (not sure if that matters wrt breaking the API).

I'm also noticing that there are two identical listeners in the repo, both with the same issue:
https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp.UI.Controls.DataGrid/Utilities/WeakEventListener.cs
https://github.com/windows-toolkit/WindowsCommunityToolkit/blob/master/Microsoft.Toolkit.Uwp/Helpers/WeakEventListener.cs

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions