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

NullReferenceException on ToolTipChanged on Windows 10 #47

Open
veler opened this issue Oct 23, 2022 · 8 comments
Open

NullReferenceException on ToolTipChanged on Windows 10 #47

veler opened this issue Oct 23, 2022 · 8 comments
Labels
bug Something isn't working

Comments

@veler
Copy link

veler commented Oct 23, 2022

Describe the bug

I'm developing on Windows 11 and test my app on Windows 10 21H2.
Everything works fine on Windows 11. However on Windows, my app crashes when I pass the mouse on the icon in the system tray.

Message:

Object reference not set to an instance of an object.

Call stack:

   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at H.NotifyIcon.TaskbarIcon.OnToolTipChange(Object sender, Boolean visible)
   at H.NotifyIcon.Core.MessageWindow.ProcessWindowMessage(UInt32 msg, WPARAM wParam, LPARAM lParam)
   at H.NotifyIcon.Core.MessageWindow.OnWindowMessageReceived(HWND hWnd, UInt32 msg, WPARAM wParam, LPARAM lParam)

Steps to reproduce the bug

  1. Be on Windows 10 build 19044.2075 (21H2) (does not reproduce on Windows 11)
  2. Using H.NotifyIcon.WinUI 2.0.68` and above.
  3. Create a windowless WinUI 3 app using this sample: https://github.com/HavenDV/H.NotifyIcon/tree/master/src/apps/H.NotifyIcon.Apps.WinUI.Windowless
  4. Update WinAppSDK to 1.2.220930.4-preview2 (I admit I didn't try with an older version)
  5. Have a XAML similar to
<tb:TaskbarIcon
        x:Key="NotifyIcon"
        Visibility="Visible"
        ToolTipText="MyTooltip"
        ContextMenuMode="SecondWindow">
        [...]
    </tb:TaskbarIcon>
  1. Start the app from Visual Studio 2022
  2. Pass the mouse above the icon in the systray, without clicking.
    image
  3. NRE exception gets raised and don't seem to be handled.
  4. App end up crashing :)

Expected behavior

No crash :) Tooltip should appear.

Screenshots

No response

NuGet package version

2.0.68
Also tried with 2.0.73

Platform

WinUI

IDE

Visual Studio 2022

Windows Version

Windows 10

WindowsAppSDK Version

Other

WindowsAppSDK Type

Packaged

Manifest

No response

Additional context

Windows 10 build 19044.2075 (21H2)

@veler veler added the bug Something isn't working label Oct 23, 2022
@justinsoderstrom
Copy link

I'm running into the same issue. A workaround I've found for now is don't provide a value for ToolTipText in the XAML.

@HavenDV
Copy link
Owner

HavenDV commented Nov 2, 2022

I really want to fix this error, but unfortunately I'm having trouble accessing my Windows 10 machine. I have a Mac M1 + Windows 11 ARM, with limited disk space.
I'd appreciate it if someone could put this issue aside by turning off Just my code and providing more information on where the NRE originated

@justinsoderstrom
Copy link

justinsoderstrom commented Nov 3, 2022

I really want to fix this error, but unfortunately I'm having trouble accessing my Windows 10 machine. I have a Mac M1 + Windows 11 ARM, with limited disk space. I'd appreciate it if someone could put this issue aside by turning off Just my code and providing more information on where the NRE originated

The NRE occurs for me TaskbarIcon.OnToolTipChange method. When it gets to the if(visible) method, the NRE occurs. The value for visible is true.

/// <summary>
/// Displays a custom tooltip, if available. This method is only
/// invoked for Windows Vista and above.
/// </summary>
/// <param name="sender"></param>
/// <param name="visible">Whether to show or hide the tooltip.</param>
private void OnToolTipChange(object? sender, bool visible)
{
    // if we don't have a tooltip, there's nothing to do here...
    if (TrayToolTipResolved == null)
    {
        return;
    }

    if (visible) // NRE HERE
    {
         ...
    }
}

IDE

Visual Studio 2022

Windows Version

Windows 10 Build 19044.2130 (21H2)

WindowsAppSDK

1.1.5

NuGet Package Version

2.0.73

Call Stack

at WinRT.Runtime.dll!WinRT.ExceptionHelpers.ThrowExceptionForHR.__Throw|20_0(int hr)	Unknown
at H.NotifyIcon.WinUI.dll!H.NotifyIcon.TaskbarIcon.OnToolTipChange(object sender, bool visible) Line 265	C#
at H.NotifyIcon.dll!H.NotifyIcon.Core.MessageWindow.ProcessWindowMessage(uint msg, Windows.Win32.Foundation.WPARAM wParam, Windows.Win32.Foundation.LPARAM lParam) Line 280	C#
at H.NotifyIcon.dll!H.NotifyIcon.Core.MessageWindow.OnWindowMessageReceived(Windows.Win32.Foundation.HWND hWnd, uint msg, Windows.Win32.Foundation.WPARAM wParam, Windows.Win32.Foundation.LPARAM lParam) Line 187	C#

Exception Details

System.NullReferenceException HResult=0x80004003 Message=Object reference not set to an instance of an object. Source=WinRT.Runtime StackTrace: at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)

WindowsAppSDK Type

Packaged

@HavenDV
Copy link
Owner

HavenDV commented Nov 3, 2022

I'll try to explain what the problem is and what my misunderstanding is:
image
image
image
According to the constants and checks, there can be no NRE here. So I would like to know the explicit state of each variable in this code on Windows 10.

@Mathyn
Copy link

Mathyn commented Nov 8, 2022

I can see something similar happening on my system (also Windows 10) with the WinUI.Windowless example project. However in my case the null reference exception is thrown as soon as it reaches line 231 in TaskBarIcon.ToolTips.cs.

This line causes issues:

TrayToolTipResolved.IsOpen = true;

While debugging I can see TrayToolTipResolved is defined so maybe the error is somewhere deeper in the IsOpen setter?

The stack trace I get

   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IToolTipMethods.set_IsOpen(IObjectReference _obj, Boolean value)  
   at H.NotifyIcon.TaskbarIcon.OnToolTipChange(Object sender, Boolean visible) in C:\\Projects\\H.NotifyIcon\\src\\libs\\H.NotifyIcon.Shared\\TaskbarIcon.ToolTips.cs:line 231"

@justinsoderstrom
Copy link

I'll try to explain what the problem is and what my misunderstanding is: image image image According to the constants and checks, there can be no NRE here. So I would like to know the explicit state of each variable in this code on Windows 10.

For my system, it never got to OnPreviewTrayToolTipOpen or OnTrayToolTipOpen methods. The moment I tried to step over the if(visible), I got the NRE. I'm not sure why that's happening for me as it doesn't make sense. I only disabled Just My Code, are there any other settings I need to enable?

As for values, I couldn't evaluate the value of TrayToolTipResolved because the method is optimized. But visible was true and sender was the following:

{H.NotifyIcon.Core.MessageWindow}
    BalloonToolTipChanged: {Method = {Void OnBalloonToolTipChanged(System.Object, Boolean)}}
    ChangeToolTipStateRequest: {Method = {Void OnToolTipChange(System.Object, Boolean)}}
    DpiChanged: {Method = {Void <.ctor>b__68_1(System.Object, System.EventArgs)}}
    HWND: 0x000511f2
    Handle: 0x000511f2
    IsCreated: true
    IsDisposed: false
    IsDoubleClick: false
    KeyboardEventReceived: {Method = {Void OnKeyboardEvent(System.Object, H.NotifyIcon.KeyboardTrayIconEventArgs)}}
    MessageHandler: {Method = {Windows.Win32.Foundation.LRESULT OnWindowMessageReceived(Windows.Win32.Foundation.HWND, UInt32, Windows.Win32.Foundation.WPARAM, Windows.Win32.Foundation.LPARAM)}}
    MouseEventReceived: {Method = {Void OnMouseEvent(System.Object, H.NotifyIcon.MouseTrayIconEventArgs)}}
    TaskbarCreated: {Method = {Void OnTaskbarCreated(System.Object, System.EventArgs)}}
    TaskbarRestartMessageId: 49360
    Version: Vista
    WindowId: "H.NotifyIcon_37686264-4b2d-4251-8586-d8f84824eb62"

@HavenDV
Copy link
Owner

HavenDV commented Nov 8, 2022

I can see something similar happening on my system (also Windows 10) with the WinUI.Windowless example project. However in my case the null reference exception is thrown as soon as it reaches line 231 in TaskBarIcon.ToolTips.cs.

This line causes issues:

TrayToolTipResolved.IsOpen = true;

While debugging I can see TrayToolTipResolved is defined so maybe the error is somewhere deeper in the IsOpen setter?

The stack trace I get

   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IToolTipMethods.set_IsOpen(IObjectReference _obj, Boolean value)  
   at H.NotifyIcon.TaskbarIcon.OnToolTipChange(Object sender, Boolean visible) in C:\\Projects\\H.NotifyIcon\\src\\libs\\H.NotifyIcon.Shared\\TaskbarIcon.ToolTips.cs:line 231"

I added a trivial check to WinUI to bypass this area. Please check 74 version on NuGet.
But I'm guessing the problem is deeper, it's just a temporary solution to bypass NRE.

@justinsoderstrom
Copy link

I can see something similar happening on my system (also Windows 10) with the WinUI.Windowless example project. However in my case the null reference exception is thrown as soon as it reaches line 231 in TaskBarIcon.ToolTips.cs.
This line causes issues:

TrayToolTipResolved.IsOpen = true;

While debugging I can see TrayToolTipResolved is defined so maybe the error is somewhere deeper in the IsOpen setter?
The stack trace I get

   at WinRT.ExceptionHelpers.<ThrowExceptionForHR>g__Throw|20_0(Int32 hr)
   at ABI.Microsoft.UI.Xaml.Controls.IToolTipMethods.set_IsOpen(IObjectReference _obj, Boolean value)  
   at H.NotifyIcon.TaskbarIcon.OnToolTipChange(Object sender, Boolean visible) in C:\\Projects\\H.NotifyIcon\\src\\libs\\H.NotifyIcon.Shared\\TaskbarIcon.ToolTips.cs:line 231"

I added a trivial check to WinUI to bypass this area. Please check 74 version on NuGet. But I'm guessing the problem is deeper, it's just a temporary solution to bypass NRE.

NRE no longer occurs for me on 74 version.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants