c# - 工具条崩溃

标签 c# .net winforms crash

我从用户那里收到我的应用程序崩溃的消息,他在事件查看器中看到了这一点:

Framework Version: v4.0.30319    
Description: The process was terminated due to an unhandled exception.
    Exception Info: System.AccessViolationException
    Stack:
       at System.Drawing.SafeNativeMethods+Gdip.GdipDrawRectangleI(System.Runtime.InteropServices.HandleRef, System.Runtime.InteropServices.HandleRef, Int32, Int32, Int32, Int32)
       at System.Drawing.Graphics.DrawRectangle(System.Drawing.Pen, Int32, Int32, Int32, Int32)
       at System.Windows.Forms.ToolStripTextBox+ToolStripTextBoxControl.WmNCPaint(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.ToolStripTextBox+ToolStripTextBoxControl.WndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Control+ControlNativeWindow.OnMessage(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
       at System.Windows.Forms.UnsafeNativeMethods.CallWindowProc(IntPtr, IntPtr, Int32, IntPtr, IntPtr)
       at System.Windows.Forms.NativeWindow.DefWndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Form.DefWndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Control.WndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.ScrollableControl.WndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Form.WndProc(System.Windows.Forms.Message ByRef)
       at DeskandArchive.MainWindow.WndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Control+ControlNativeWindow.OnMessage(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.Control+ControlNativeWindow.WndProc(System.Windows.Forms.Message ByRef)
       at System.Windows.Forms.NativeWindow.Callback(IntPtr, Int32, IntPtr, IntPtr)
       at System.Windows.Forms.UnsafeNativeMethods.PeekMessage(MSG ByRef, System.Runtime.InteropServices.HandleRef, Int32, Int32, Int32)
       at System.Windows.Forms.Application+ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr, Int32, Int32)
       at System.Windows.Forms.Application+ThreadContext.RunMessageLoopInner(Int32, System.Windows.Forms.ApplicationContext)
       at System.Windows.Forms.Application+ThreadContext.RunMessageLoop(Int32, System.Windows.Forms.ApplicationContext)
       at System.Windows.Forms.Application.Run(System.Windows.Forms.Form)
       at A.cc1462b28845fccfe3634174d36bc619a.ce1b648a1c328cbefe2529fb98bf21b8c()

据我在互联网上的搜索,以及我可以从中读到的内容,还包括如果我的代码崩溃,它应该显示对话框来报告用户没有看到的错误,我得出的结论是,这是.NET Framework 中绘制 Toolstrip 时的错误。

我说得对吗?有什么办法可以解决这个问题吗?我已阅读有关重新安装 .NET Framework 的建议,但大多数人报告说它没有帮助?

最佳答案

崩溃的不是 ToolStrip,而是 GDI+。 GDI+ 是一大块非托管代码,早在 .NET 出现之前就已存在,System.Drawing 是带有包装器类的 namespace 。与此堆栈跟踪中使用的 Graphics 类一样。

让 GDI+ 崩溃的情况非常罕见,.NET 包装类非常擅长防止将错误参数传递给 GDI+ 的函数。获得这样的 AccessViolation 需要破坏 GDI+ 使用的堆。堆被非托管代码中的指针错误损坏。那可能是您的代码,值得注意的是您正在覆盖 WndProc()。但更典型的是一些应用程序将自己注入(inject)到您的流程中。要么是有意的,就像病毒扫描程序一样,要么是无意中像使用 OpenFileDialog 时加载的 shell 扩展处理程序。这样的扩展也可以使用 GDI+,这很常见,或者只是有一个坏指针,可以在任何地方喷射垃圾。

您永远不会从托管堆栈跟踪中发现,损坏早在 GDI+ 崩溃之前就已经完成。这使得诊断堆损坏错误变得异常困难,这也是 Java 和 .NET 成为非常流行的平台的一个重要原因。当您不能完全复制用户的运行时环境时,这就变得不可能了。像这样的错误几乎总是以“不重现”来关闭,就像你将它提交给 Microsoft 并且仅将堆栈跟踪作为证据一样。是否追求重现场景取决于您,但请注意,您会遇到很多困难。告诉用户最好的事情是使用另一台机器,或者通过卸载不必要的或有风险的程序来使她的机器再次稳定。特别是 shell 扩展。

关于c# - 工具条崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13100011/

相关文章:

c# - WPF如何让拖放与控制键一起使用?

.net - Microsoft 异常处理 block - 这不是过度设计的完美示例吗?

c# - 使 slider 使用现有的 IValueConverter

c# - 无法在设计模式下打开表单

c# - 在编码的 UI 测试中仅知道部分窗口标题时查找窗口

c# - 在另一个表单 [C#] 上插入详细信息后,DataGridView 不更新

c# - 在 C# 中使用 FakeItEasy 制作假 MongoDB 集合

c# - 加载 X509Certificate2 结束并出现 Windows Server 2012 上发生内部错误

c# - 从右到左 TabControl c# 的 TabPages 的关闭按钮

vb.net - 将十六进制颜色字符串转换为 RGB 颜色