常见原因 - .NET 下的访问冲突错误 [英] Common causes of - Access Violation errors under .NET

查看:22
本文介绍了常见原因 - .NET 下的访问冲突错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找 .NET 下访问冲突错误的常见原因.

I'm looking for common causes of Access Violation errors under .NET.

到目前为止我检查过的东西 -

Things I have checked so far -

  • 对所有实现 IDisposable 的对象调用 Dispose
  • 检查调用 COM 对象的有效参数
  • 明确删除所有手动添加的事件处理程序
  • 不要显式调用 GC.Collect/GC.WaitForPendingFinalizers
  • 在处理本机对象(位图等)时添加和消除内存压力(添加)
  • 验证所有 PInvoke 调用的有效参数类型
  • 确保正确使用 IntPtr、SafeHandle 和 HandleRef
  • 线程(线程安全,可重入函数),正确使用等待句柄.(已添加)
  • 确保应用程序和 DLL 都针对同一平台(x86 或 x64)(应用程序和 dll 应针对与 COM 对象相同的平台.)(已添加)
  • Call Dispose on all objects implementing IDisposable
  • Check for valid arguments in calls to COM objects
  • Explicitly remove all manually added event handlers
  • DO NOT explicity call GC.Collect/GC.WaitForPendingFinalizers
  • Add and Remove memory pressure when dealing with native objects (Bitmap, etc..) (Added)
  • Verify all PInvoke calls for valid argument types
  • Ensure proper use of IntPtr, SafeHandle and HandleRef
  • Threading (Thread Safe, Reentrant functions), proper use of waithandles. (Added)
  • Ensure application and DLL's are all targetting the same platform (x86 or x64) (The application and dll's should target the same platform as the COM objects.) (Added)

还有其他建议吗?

编辑 - 将故障转储分析移至不同的问题.

Edit - Moved crash dump analysis to different question.

推荐答案

任何不安全或非托管代码的使用都会导致此类异常.

Any use of unsafe or unmanaged code can get you that type of exceptions.

AccessViolationException:

访问冲突发生在非托管或不安全的代码,当代码尝试读取或写入内存尚未分配的,或它无权访问.这个通常发生是因为一个指针有一个价值不佳.并非所有读取或写入通过坏指针导致访问违规,因此是访问违规通常表示多次读取或写入已通过坏指针,并且该内存可能是损坏.因此,访问违规几乎总是表示严重编程错误.在 .NET 中框架版本 2.0,一个AccessViolationException 明确识别这些严重错误.

An access violation occurs in unmanaged or unsafe code when the code attempts to read or write to memory that has not been allocated, or to which it does not have access. This usually occurs because a pointer has a bad value. Not all reads or writes through bad pointers lead to access violations, so an access violation usually indicates that several reads or writes have occurred through bad pointers, and that memory might be corrupted. Thus, access violations almost always indicate serious programming errors. In the .NET Framework version 2.0, an AccessViolationException clearly identifies these serious errors.

在完全由可验证的托管代码,所有引用要么有效要么为空,和访问冲突是不可能的.发生 AccessViolationException仅当可验证的托管代码与非托管代码或与不安全的托管代码.

In programs consisting entirely of verifiable managed code, all references are either valid or null, and access violations are impossible. An AccessViolationException occurs only when verifiable managed code interacts with unmanaged code or with unsafe managed code.

推荐:

GFlags应用程序验证器

这篇关于常见原因 - .NET 下的访问冲突错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆