question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[BUG] TrayIcon crashes application on closing (Windows)

See original GitHub issue

Describe 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

  1. Create a new mvvm application
  2. Set it up as a fluent design application
  3. Add a tray icon to the application (See additional context)
  4. Start the application via Debug/Release
  5. Close the application with the close button
  6. 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:closed
  • Created 2 years ago
  • Reactions:1
  • Comments:18 (6 by maintainers)

github_iconTop GitHub Comments

2reactions
maxkatz6commented, Feb 13, 2022

This was fixed in master

1reaction
tsuckowcommented, May 20, 2022

I present you the workaround. Why it doesn’t register itself I don’t know.

public override void OnFrameworkInitializationCompleted()
{
    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
    {
        desktop.MainWindow = new MainWindow();

        var trayIcon = new TrayIcon();
        trayIcon.Icon = desktop.MainWindow.Icon;
        trayIcon.ToolTipText = "Test";

        // Workaround for https://github.com/AvaloniaUI/Avalonia/issues/7588
        var icons = new TrayIcons();
        icons.Add(trayIcon);
        TrayIcon.SetIcons(Application.Current!, icons);
    }

    base.OnFrameworkInitializationCompleted();
}
Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found