Skip to content

Exception:算术运算导致溢出。 #886

Closed

Description

@cuiliang

这不是一个HC的bug,但是应该可以在HC里加以规避处理。

现象
在个别人的电脑上会出现一个奇怪的异常:

OverflowException
   在 System.Windows.Shell.WindowChromeWorker._HandleNCHitTest(WM uMsg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 System.Windows.Shell.WindowChromeWorker._WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 System.Windows.Interop.HwndSource.PublicHooksFilterMessage(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndWrapper.WndProc(IntPtr hwnd, Int32 msg, IntPtr wParam, IntPtr lParam, Boolean& handled)
   在 MS.Win32.HwndSubclass.DispatcherCallbackOperation(Object o)
   在 System.Windows.Threading.ExceptionWrapper.InternalRealCall(Delegate callback, Object args, Int32 numArgs)
   在 System.Windows.Threading.ExceptionWrapper.TryCatchWhen(Object source, Delegate callback, Object args, Int32 numArgs, Delegate catchHandler)

相关信息
在StackOverflow上找到一个相关的帖子:
https://stackoverflow.com/questions/33287542/what-would-cause-wm-nchittest-lparam-to-overflow-a-32-bit-integer

里面有人贴了一个一个网址:
https://developercommunity.visualstudio.com/t/overflow-exception-in-windowchrome/167357

提到的解决方法:

protected override void OnSourceInitialized( EventArgs e )
      {
         base.OnSourceInitialized( e );
         ( (HwndSource)PresentationSource.FromVisual( this ) ).AddHook( HookProc );
      }
      private IntPtr HookProc( IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled )
      {
         if ( msg == 0x0084 /*WM_NCHITTEST*/ )
         {
            // This prevents a crash in WindowChromeWorker._HandleNCHitTest
            try
            {
               lParam.ToInt32();
            }
            catch ( OverflowException )
            {
               handled = true;
            }
         }
         return IntPtr.Zero;
      }

其它相关链接:

Activity

  1. NaBian commented on Jun 28, 2021

    @NaBian
    Member

    重写 OnSourceInitialized 方法确认过可以解决这个问题吗?

  2. cuiliang commented on Jun 28, 2021

    @cuiliang
    ContributorAuthor

    看起来是可以的。目前hc:Window类里已经有HookProc,我觉得加上这个判断应该不会产生额外的负担却可以避免崩溃,还是值得的。

  3. NaBian commented on Jun 28, 2021

    @NaBian
    Member

    ok

  4. lindexi commented on Jul 12, 2022

    @lindexi
    Contributor

    我感觉这是应该在 WPF 框架处理的,我准备在底层处理

  5. added a commit that references this issue on Mar 28, 2024
    a879ebc
  6. dlgcy commented on Aug 8, 2024

    @dlgcy

    我感觉这是应该在 WPF 框架处理的,我准备在底层处理

    在哪个版本的 .NET 中修复了?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions

      ⌂ 换网址