[BUG] TrayIcon crashes application on closing (Windows)
See original GitHub issueDescribe the bug Application crashed on Windows if you exit and there is a tray icon on the main window
To Reproduce Not sure if this happens on other operation systems as well only tested it on Windows
- Create a new mvvm application
- Set it up as a fluent design application
- Add a tray icon to the application (See additional context)
- Start the application via Debug/Release
- Close the application with the close button
- Crash on
program.cs->System.NullReferenceException: 'Object reference not set to an instance of an object.'
Expected behavior The application closes successfully
Desktop (please complete the following information):
- OS Windows 10
- Avalonia Version 0.10.12
Additional context Tray icon code
<TrayIcon.Icons>
<TrayIcons>
<TrayIcon Icon="/Assets/avalonia-logo.ico" Command="{Binding ShowApplicationCommand}" ToolTipText="Modular tool manager">
<TrayIcon.Menu>
<NativeMenu>
<NativeMenuItem Header="Testing" Command="{Binding ExitApplicationCommand}"/>
</NativeMenu>
</TrayIcon.Menu>
</TrayIcon>
</TrayIcons>
</TrayIcon.Icons>
Stacktrace
System.NullReferenceException
HResult=0x80004003
Message=Object reference not set to an instance of an object.
Source=Avalonia.Controls
StackTrace:
at Avalonia.Controls.TrayIcon.RemoveIcons(IEnumerable`1 icons) in /_/src/Avalonia.Controls/TrayIcon.cs:line 204
at Avalonia.Controls.TrayIcon.Lifetime_Exit(Object sender, ControlledApplicationLifetimeExitEventArgs e) in /_/src/Avalonia.Controls/TrayIcon.cs:line 195
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.DoShutdown(ShutdownRequestedEventArgs e, Boolean force, Int32 exitCode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 173
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.TryShutdown(Int32 exitCode) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 91
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.HandleWindowClosed(Window window) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 82
at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.WindowClosedEvent(Object sender, RoutedEventArgs e) in /_/src/Avalonia.Controls/ApplicationLifetimes/ClassicDesktopStyleApplicationLifetime.cs:line 33
at Avalonia.Interactivity.RoutedEvent.<>c__DisplayClass23_0.<AddClassHandler>b__0(ValueTuple`2 args) in /_/src/Avalonia.Interactivity/RoutedEvent.cs:line 94
at System.Reactive.AnonymousObserver`1.OnNextCore(T value) in /_/Rx.NET/Source/src/System.Reactive/AnonymousObserver.cs:line 67
at System.Reactive.ObserverBase`1.OnNext(T value) in /_/Rx.NET/Source/src/System.Reactive/ObserverBase.cs:line 36
at System.Reactive.Subjects.Subject`1.OnNext(T value) in /_/Rx.NET/Source/src/System.Reactive/Subjects/Subject.cs:line 147
at Avalonia.Interactivity.EventRoute.RaiseEventImpl(RoutedEventArgs e) in /_/src/Avalonia.Interactivity/EventRoute.cs:line 148
at Avalonia.Interactivity.EventRoute.RaiseEvent(IInteractive source, RoutedEventArgs e) in /_/src/Avalonia.Interactivity/EventRoute.cs:line 87
at Avalonia.Interactivity.Interactive.RaiseEvent(RoutedEventArgs e) in /_/src/Avalonia.Interactivity/Interactive.cs:line 122
at Avalonia.Controls.Window.HandleClosed() in /_/src/Avalonia.Controls/Window.cs:line 942
at Avalonia.Win32.WindowImpl.AppWndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/WindowImpl.AppWndProc.cs:line 86
at Avalonia.Win32.WindowImpl.WndProc(IntPtr hWnd, UInt32 msg, IntPtr wParam, IntPtr lParam) in /_/src/Windows/Avalonia.Win32/WindowImpl.WndProc.cs:line 30
Issue Analytics
- State:
- Created 2 years ago
- Reactions:1
- Comments:18 (6 by maintainers)
Top Results From Across the Web
Tray Icon does not disappear when the program is closed ...
Little Guess:If an application is forcefully terminated (e.g. through Task Manager), then Windows does not remove the notification icon.
Read more >Why does system try icon stay after application crashes or ...
The icons stay around because the Application didn't remove them. Notification Area icons are not associated with Applications. Applications ...
Read more >windows 10 system tray app crashing after several hours of ...
Exit code 0xC0000409 is STATUS_STACK_BUFFER_OVERRUN , and the error message does say a stack overflow occured. That is most commonly caused by ...
Read more >Application is crashed on an attempt to close it via ...
When user tries to exit the application using tray icon context menu, sometimes (about 1 in 15) the application crashes with non-.net (native) ......
Read more >Bug report: tray icon disappears when explorer.exe crashes
When Deluge is minimized to tray, and the icon is not there, I can't access it nor close it (unless by telling windows...
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

This was fixed in master
I present you the workaround. Why it doesn’t register itself I don’t know.