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

Unhandled exception during tray icon creation on Linux #18156

Open
Shivansps opened this issue Feb 9, 2025 · 0 comments
Open

Unhandled exception during tray icon creation on Linux #18156

Shivansps opened this issue Feb 9, 2025 · 0 comments

Comments

@Shivansps
Copy link

Shivansps commented Feb 9, 2025

Describe the bug

I have a code to create the tray icon when the mainwindow is closing that works ok on both Windows and MacOS, but crashes on Linux like 90% of the time.
Now me dev enviroment is on Windows so it is hard for me to tell what exactly causes the crash, but from what i see its random as sometimes the icon is created, but will likely crash after opening the main window and closing it again.

This is what is sent to the console:

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
   at Tmds.DBus.SourceGenerator.ComCanonicalDbusmenu.EmitLayoutUpdated(UInt32 revision, Int32 parent)
   at Avalonia.FreeDesktop.DBusMenuExporter.DBusMenuExporterImpl.DoLayoutReset()
   at Avalonia.Threading.DispatcherOperation.InvokeCore()
   at Avalonia.Threading.DispatcherOperation.Execute()
   at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job)
   at Avalonia.Threading.Dispatcher.ExecuteJobsCore(Boolean fromExplicitBackgroundProcessingCallback)
   at Avalonia.Threading.Dispatcher.Signaled()
   at Avalonia.X11.X11PlatformThreading.CheckSignaled()
   at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken)
   at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)
   at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
   at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
   at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.StartCore(String[] args)
   at Knossos.NET.Program.Main(String[] args)
Abortado (`core' generado)

in other pc, this is when i tried switching the visibility from true to false and to true again

Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
  at Tmds.DBus.SourceGenerator.OrgKdeStatusNotifierItem.EmitNewTitle()
  at Avalonia.FreeDesktop.StatusNotifierItemDbusObj.InvalidateAll()
  at Avalonia.FreeDesktop.StatusNotifierItemDbusObj.SetTitleAndTooltip(String text)
  at Avalonia.FreeDesktop.DBusTrayIconImpl.CreateTrayIcon()
  at System.Threading.Tasks.Task.<>c.<ThrowAsync>b__128_0(Object state)
  at Avalonia.Threading.SendOrPostCallbackDispatcherOperation.InvokeCore()
  at Avalonia.Threading.DispatcherOperation.Execute()
  at Avalonia.Threading.Dispatcher.ExecuteJob(DispatcherOperation job)
  at Avalonia.Threading.Dispatcher.ExecuteJobsCore(Boolean fromExplicitBackgroundProcessingCallback)
  at Avalonia.Threading.Dispatcher.Signaled()
  at Avalonia.X11.X11PlatformThreading.CheckSignaled()
  at Avalonia.X11.X11PlatformThreading.RunLoop(CancellationToken cancellationToken)
  at Avalonia.Threading.DispatcherFrame.Run(IControlledDispatcherImpl impl)
  at Avalonia.Threading.Dispatcher.PushFrame(DispatcherFrame frame)
  at Avalonia.Threading.Dispatcher.MainLoop(CancellationToken cancellationToken)
  at Avalonia.Controls.ApplicationLifetimes.ClassicDesktopStyleApplicationLifetime.StartCore(String[] args)
  at Knossos.NET.Program.Main(String[] args)

Im disposing the tray icon when the main window is show and re-creating it when it is closed (because menu items needs updating), i also tried not to dispose it, create it once and just clear the items and toggle its visibility. But that also causes it to crash.

To Reproduce

On app.axaml.cs

TrayIcon? trayIcon = null;

public override void OnFrameworkInitializationCompleted()
{
    if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
    {
          desktop.MainWindow = new MainWindow
          {
              DataContext = new MainWindowViewModel()
          };
          desktop.MainWindow.Closing += (_, __) =>
          {
                  desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                  StartTrayIcon();
          };
   }
    base.OnFrameworkInitializationCompleted();
}

private async void StartTrayIcon()
{
  trayIcon?.Dispose();
  trayIcon = new TrayIcon
  {
      IsVisible = true,
      ToolTipText = "test",
      Icon = new WindowIcon(new Bitmap(AssetLoader.Open(new Uri("avares://{app}/Assets/avalonia-icon.ico")))),
      Menu = new NativeMenu() { new NativeMenuItem("Loading...") }
  };

  while (!initIsComplete) { await Task.Delay(10); }

  trayIcon.Menu?.Items.Clear();

  var open = new NativeMenuItem("Open");
  open.Click += (s, _) => {
      if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
      {
            desktop.MainWindow = new MainWindow
            {
                DataContext = new MainWindowViewModel()
            };

            desktop.MainWindow.Closing += (_, __) =>
            {
                    desktop.ShutdownMode = ShutdownMode.OnExplicitShutdown;
                    StartTrayIcon();
            };

          desktop.MainWindow.Show();
          desktop.ShutdownMode = ShutdownMode.OnLastWindowClose;
          trayIcon?.Dispose();
          GC.Collect();
      }
  };
  trayIcon.Menu?.Add(open);
  trayIcon.Menu?.Add(new NativeMenuItemSeparator());

// Menu and submenus here

  trayIcon.Menu?.Add(new NativeMenuItemSeparator());
  var close = new NativeMenuItem("Exit");
  close.Click += (s, e) => { 
      if (ApplicationLifetime is IClassicDesktopStyleApplicationLifetime desktop)
      {
          trayIcon?.Dispose();
          desktop.Shutdown();
      }
  };
  trayIcon.Menu?.Add(close);

  GC.Collect();
}

Expected behavior

No response

Avalonia version

11.2.3

OS

Linux

Additional context

Making a close to try function

The full code in question is here:
https://github.com/KnossosNET/Knossos.NET/blob/main/Knossos.NET/App.axaml.cs

@Shivansps Shivansps added the bug label Feb 9, 2025
@MrJul MrJul added the os-linux label Feb 9, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants