H.NotifyIcon icon indicating copy to clipboard operation
H.NotifyIcon copied to clipboard

Memory error reading bug when using ContextMenuMode="SecondWindow" for tray icons in Windows AppSdk 1.4.230822000 version

Open BlameTwo opened this issue 1 year ago • 9 comments

Describe the bug

Error: Exception thrown at 0x00007FFDC50BDE84 (Microsoft. UI. Windowing. Core. dll) (located in App1. exe): 0xC000000 5: An access violation occurred while reading position 0x0000000000030.

Steps to reproduce the bug

In WIndowsAppSdk (v1.4.230822000), use the TaskbarIcon of H.NotifyIcon. WinUI (v2.0.116) to create a tray icon, then set ContextMenuMode="SecondWindow", and finally trigger Exception throw at 0x00007FFDC50BDE84 (Microsoft. UI. Windowing. Core. dll) (located in App. WinUI3 Exe): 0xC000000 5: An access Violation occurred while reading position 0x000000000030

Expected behavior

Should be created correctly and functioning properly

Screenshots

No response

NuGet package version

WindowsAppSdk (1.4.230822000) H.NotifyIcon(2.0.116)

Platform

WinUI

IDE

Visual Studio 2022-preview

Windows Version

Windows 11

WindowsAppSDK Version

Other

WindowsAppSDK Type

None

Manifest

No response

Additional context

No response

BlameTwo avatar Sep 11 '23 06:09 BlameTwo

@HavenDV

BlameTwo avatar Sep 14 '23 14:09 BlameTwo

Sorry, I now have time issues and don’t have access to a Windows machine. I had problems running WinUI on a virtual machine inside Parallels Desktop arm64 SecondWindow is experimental in nature and has a lot of winapi calls to implement transparency and borderless window, and so it's quite time consuming to test all the options and fix it for now for me. I'd be happy to accept PR if you're interested in doing it. The WinUI code for SecondWindow is concentrated in one file and, I hope, is quite clear - https://github.com/HavenDV/H.NotifyIcon/blob/master/src/libs/H.NotifyIcon.Shared/TaskbarIcon.ContextMenu.WinUI.SecondWindow.cs

HavenDV avatar Sep 14 '23 14:09 HavenDV

It actually crashes when instantiating a new window here: In TaskbarIcon.ContextMenu.WinUI.SecondWindow.cs image

I am not very experienced with .net, but I think this likely is a runtime bug. Instantiating without passing any arguments also results in a crash.

KarelChanivecky avatar Sep 14 '23 19:09 KarelChanivecky

It would be great if someone took the time to check if rolling back to one of the previous versions of WindowsAppSDK helps

HavenDV avatar Sep 14 '23 19:09 HavenDV

1.3 works. We got some other issue with 1.3 so it would've been nice if this worked, but that's life.

I tried the popup message mode, but it seems that the click events aren't being triggered? Do you have a clue on that?

The active window mode works, but it renders behind the system tray blob.

KarelChanivecky avatar Sep 14 '23 19:09 KarelChanivecky

In the latest package version, I found that this issue still occurs in the debugging mode of Visual Studio 2022, but I tried to run it directly without debugging and found that it also has issues. And I added global exception capture in the App. xaml file, which doesn't seem to affect the program's operation, but it will hinder me from debugging the application.

BlameTwo avatar Sep 19 '23 08:09 BlameTwo

Same problem here, the application crashes in debugging mode after upgrading the WindowsAppSDK to 1.4. It worked fine with 1.3.

Jimex avatar Oct 08 '23 05:10 Jimex

Please try to disable In app toolbar in VS

tibitoth avatar Oct 12 '23 10:10 tibitoth

The temporary solution is to build the ContextFlyout programmatically and not assign it to the TrayIcon control.

            var menuFlyout = new MenuFlyout();
            var showHideWinCmd = new XamlUICommand()
            {
                Label = TranslateUtils.Translate("menu_show_hide_window"),
                Description = TranslateUtils.Translate("menu_show_hide_window"),
                IconSource = new SymbolIconSource() { Symbol = Symbol.OpenPane },

            };
            showHideWinCmd.ExecuteRequested += ShowHideWindowCommand_ExecuteRequested;
            menuFlyout.Items.Add(new MenuFlyoutItem()
            {
                Command = showHideWinCmd
            });

            var exitAppCmd = new XamlUICommand()
            {
                Label = TranslateUtils.Translate("menu_exit_application"),
                Description = TranslateUtils.Translate("menu_exit_application"),
                IconSource = new SymbolIconSource() { Symbol = Symbol.ClosePane },

            };
            showHideWinCmd.ExecuteRequested += ExitApplicationCommand_ExecuteRequested;
            menuFlyout.Items.Add(new MenuFlyoutItem()
            {
                Command = exitAppCmd
            });

            TrayIcon.LeftClickCommand = showHideWinCmd;
#if !DEBUG
            TrayIcon.ContextFlyout = menuFlyout;
#endif            

Jimex avatar Oct 26 '23 02:10 Jimex