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

Unsubscribing from a WinRT event sometimes does not work. #753

Closed
kmahone opened this issue Feb 26, 2021 · 1 comment · Fixed by #759
Closed

Unsubscribing from a WinRT event sometimes does not work. #753

kmahone opened this issue Feb 26, 2021 · 1 comment · Fixed by #759
Assignees
Labels
bug Something isn't working fixed Issue has been fixed in an upcoming or existing release
Milestone

Comments

@kmahone
Copy link
Member

kmahone commented Feb 26, 2021

Sometimes unsubscribing from a WinRT event in CsWinRT has no effect. The event handler will continue to get called even after the app has unsubscribed from the event.

It seems to be caused by an interaction with GC.

It seems to primarily (only?) affect static events rather than instance events.

I have attached a repro app that demonstrates this problem.

To repro the issue:
Create a new WinUI 3 C# Desktop app. Update it with the code from the attached .cs and .xaml file.
Build and launch the app.
The buttons "Add Handler" and "Remove Handler" add and remove an event handler for FocusManager.GettingFocus.
Click Add Handler, and tab around the app. Notice that GettingFocus fires as expected.
If Add Handler is clicked

multiple times, the handler will be applied multiple times as expected. Tabbing around the app will show GettingFocus being handled multiple times.
Click Add and Remove multiple times and tab around the app and

see that everything behaves as expected.

Toggle "GC Timer" on. This will cause GC.Collect to get called periodically.
Now the behavior of the GettingFocus starts behaving poorly. Often times "Remove Handler" appears to have no effect. Even if you completely unsubscribe from the GettingFocus event, the handler continues to be called.

MainWindow.xaml
MainWindow.xaml.cs

@kmahone kmahone added the bug Something isn't working label Feb 26, 2021
@manodasanW manodasanW added this to the Release 1.1.4 milestone Feb 27, 2021
@manodasanW
Copy link
Member

Was able to get a repro, looks like we use a weak reference to store the static singleton, but that is getting freed during GC. This typically would be fine, but I think that is causing issues when combined with the fact we register a wrapper delegate rather than delegates 1:1.

@manodasanW manodasanW self-assigned this Mar 5, 2021
@angelazhangmsft angelazhangmsft added the fixed Issue has been fixed in an upcoming or existing release label May 13, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Issue has been fixed in an upcoming or existing release
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants