查找 System.AccessViolationException 的原因 [英] Finding the cause of System.AccessViolationException
问题描述
我们的应用程序遇到了奇怪的致命 System.AccessViolationException.我们看到这些是因为我们已经配置了 AppDomain.CurrentDomain.UnhandledException 事件来记录异常.
Our application experiences the odd fatal System.AccessViolationException. We see these as we've configured the AppDomain.CurrentDomain.UnhandledException event to log the exception.
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:uild-dirsrcBootstrap.cs:line 25
异常本身似乎没有比消息尝试读取或写入受保护的内存.这通常表明其他内存已损坏."
The exception itself doesn't seem to contain any more information than the message "Attempted to read or write protected memory. This is often an indication that other memory is corrupt."
- 我们现在可以采取哪些步骤来找出问题的原因?
- 有没有办法确定导致崩溃的非法地址或指针值?
- 我们能否找出导致问题的本机库代码?
- 我们可以启用更多调试/跟踪吗?
更新
- 这可能是由于早期对 WinForms API 的非线程安全使用造成的吗?
推荐答案
您所遇到的与程序遇到问题,现在将关闭"完全相同,只是它被 .NET 运行时捕获,而不是比操作系统.
What you are experiencing is the exact equivalent to "The program has experienced a problem and will now close", except it's being caught by the .NET runtime, rather than the OS.
查看堆栈跟踪,它不是由您的代码触发的,这让我认为它来自您正在使用的库或自定义控件生成的工作线程.
Looking at the stack trace, it's not being triggered by your code, which makes me think that it's coming from a worker thread spawned by a library you're using or a custom control.
跟踪此类事件的唯一方法是在调试器下运行本机库,它应该在访问冲突冒泡到 CLR 层之前捕获访问冲突.这可以很容易也可以很困难.
The only way to track something like this would be to run the native libraries under a debugger, which should trap the access violation before it bubbles up to the CLR layer. This can be easy or hard.
如果本机代码是您自己的项目,那么最简单的设置方法是将 .NET 项目和 C++ 项目放在同一个解决方案中,并确保 .NET 项目引用 C++ 项目.如果您发布有关您的环境的更多详细信息,我可能会提供更具体的建议.
If the native code is your own project, then the easiest way to set this up is to put both the .NET project and the C++ project in the same solution, and ensure that the .NET project is referencing the C++ project. If you post more details about your environment, I may be able to give more specific advice.
这篇关于查找 System.AccessViolationException 的原因的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!