Skip to content

[Watson] clr20r3: CLR_EXCEPTION_System.Collections.Generic.KeyNotFoundException_80131577_PresentationCore.dll!MS.Internal.DpiUtil+DpiAwarenessScope..ctor #1198

Closed
@grubioe

Description

@grubioe

Copied from ADO ID 893177

  • Does the bug reproduce also in WPF for .NET Framework 4.8?: Yes

We have a bug here - a missing check.

\WPF\src\Core\CSharp\MS\Internal\DpiUtil\DpiUtil+DpiAwarenessScope.cs

            [SecuritySafeCritical]
            private DpiAwarenessScope(
                DpiAwarenessContextValue dpiAwarenessContextValue,
                bool updateIfThreadInMixedHostingMode,
                bool updateIfWindowIsSystemAwareOrUnaware,
                IntPtr hWnd)
            {
                if (!OperationSupported)
                {
                    return;
                }

                if (updateIfThreadInMixedHostingMode && !this.IsThreadInMixedHostingBehavior)
                {
                    return;
                }

                if (updateIfWindowIsSystemAwareOrUnaware &&
                    (hWnd == IntPtr.Zero || !this.IsWindowUnawareOrSystemAware(hWnd)))
                {
                    return;
                }

                try
                {
                    this.OldDpiAwarenessContext = 
                        UnsafeNativeMethods.SetThreadDpiAwarenessContext(
                            new DpiAwarenessContextHandle(dpiAwarenessContextValue));
                }
                catch (Exception e) when (e is EntryPointNotFoundException || e is MissingMethodException || e is DllNotFoundException)
                {
                    OperationSupported = false;
                }
            }

We should either be catch KeyNotFoundException, or better yet, checking for dpiAwarenessContextValue==Invalid and return with OperationSupported=false early in the constructor.

Found from Watson data:

Call Stack

  1. mscorlib.ni!System.Collections.Generic.Dictionary_2[[MS.Utility.DpiAwarenessContextValue,_WindowsBase],[System.IntPtr,_mscorlib]].get_Item
  2. at PresentationCore.ni!MS.Internal.DpiUtil in DpiUtil+DpiAwarenessScope.cs
  3. at PresentationFramework.ni!System.Windows.SystemResources.CreateResourceChangeListenerWindow in SystemResources.cs
  4. at PresentationFramework.ni!System.Windows.SystemResources.EnsureResourceChangeListener in SystemResources.cs
  5. at PresentationFramework.ni!System.Windows.SystemResources.GetDpiAwarenessCompatibleNotificationWindow in SystemResources.cs
  6. at PresentationFramework.ni!System.Windows.Interop.HwndHost.BuildOrReparentWindow in HwndHost.cs
  7. at PresentationFramework.ni!System.Windows.Interop.HwndHost.OnSourceChanged in HwndHost.cs
  8. at PresentationCore.ni!System.Windows.SourceChangedEventArgs.InvokeEventHandler in SourceChangedEventArgs.cs
  9. at PresentationCore.ni!System.Windows.RoutedEventArgs.InvokeHandler in RoutedEventArgs.cs
  10. at PresentationCore.ni!System.Windows.RoutedEventHandlerInfo.InvokeHandler in RoutedEventHandlerInfo.cs
  11. at PresentationCore.ni!System.Windows.EventRoute.InvokeHandlersImpl in EventRoute.cs
  12. at PresentationCore.ni!System.Windows.UIElement.RaiseEventImpl in UIElement.cs
  13. at PresentationCore.ni!System.Windows.UIElement.RaiseEvent in UIElement.cs
  14. at PresentationCore.ni!System.Windows.PresentationSource.UpdateSourceOfElement in PresentationSource.cs
  15. at PresentationCore.ni!System.Windows.PresentationSource.RootChanged in PresentationSource.cs
  16. at PresentationCore.ni!System.Windows.Interop.HwndSource.set_RootVisualInternal in HwndSource.cs
  17. at PresentationCore.ni!System.Windows.Interop.HwndSource.Dispose in HwndSource.cs
  18. at PresentationCore.ni!System.Windows.Interop.HwndSource.OnHwndDisposed in HwndSource.cs
  19. at WindowsBase.ni!MS.Win32.HwndWrapper.Dispose in HwndWrapper.cs
  20. at WindowsBase.ni!MS.Win32.HwndWrapper.WndProc in HwndWrapper.cs
  21. at WindowsBase.ni!MS.Win32.HwndSubclass.DispatcherCallbackOperation in HwndSubclass.cs
  22. at WindowsBase.ni!System.Windows.Threading.ExceptionWrapper.InternalRealCall in ExceptionWrapper.cs
  23. at WindowsBase.ni!System.Windows.Threading.ExceptionWrapper.TryCatchWhen in ExceptionWrapper.cs
  24. at WindowsBase.ni!System.Windows.Threading.Dispatcher.LegacyInvokeImpl in Dispatcher.cs
  25. at WindowsBase.ni!MS.Win32.HwndSubclass.SubclassWndProc in HwndSubclass.cs

Metadata

Metadata

Labels

BugProduct bug (most likely)tell-modeIssues and PR's that require notice to .NET Core Shiproom

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions