我们的应用程序遇到奇怪的致命 System.AccessViolationException。我们在配置 AppDomain.CurrentDomain.UnhandledException 事件以记录异常时看到这些。
Exception: System.AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(Form mainForm)
at Bootstrap.Run() in e:\build-dir\src\Bootstrap.cs:line 25
除了消息“尝试读取或写入 protected 内存。这通常表明其他内存已损坏。”
,异常本身似乎没有包含更多信息。- 我们现在可以采取哪些步骤来找出问题的原因?
- 有什么方法可以确定导致崩溃的非法地址或指针值吗?
- 我们能否找出导致问题的 native 库代码?
- 是否有更多我们可以启用的调试/跟踪?
更新
- 这会不会是由于早期对 WinForms API 的非线程安全使用导致的?
最佳答案
您遇到的情况与“程序遇到问题,现在将关闭”完全相同,只是它被 .NET 运行时而非操作系统捕获。
查看堆栈跟踪,它不是由您的代码触发的,这让我认为它来自您正在使用的库或自定义控件生成的工作线程。
跟踪此类事件的唯一方法是在调试器下运行 native 库,它应该在访问冲突冒泡到 CLR 层之前捕获它。这可能很容易也可能很难。
如果 native 代码是您自己的项目,那么最简单的设置方法是将 .NET 项目和 C++ 项目放在同一个解决方案中,并确保 .NET 项目引用 C++ 项目。如果您发布有关您的环境的更多详细信息,我可能会提供更具体的建议。
关于c# - 查找 System.AccessViolationException 的原因,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5133971/