You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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.
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.
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
The text was updated successfully, but these errors were encountered: