System Namespace


.NET Framework Class Library
AccessViolationException Class

The exception that is thrown when there is an attempt to read or write protected memory.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
<SerializableAttribute> _
<ComVisibleAttribute(True)> _
Public Class AccessViolationException _
    Inherits SystemException
Visual Basic (Usage)
Dim instance As AccessViolationException
C#
[SerializableAttribute]
[ComVisibleAttribute(true)]
public class AccessViolationException : SystemException
Visual C++
[SerializableAttribute]
[ComVisibleAttribute(true)]
public ref class AccessViolationException : public SystemException
JScript
public class AccessViolationException extends SystemException
Remarks

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.

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.

Version Information

This exception is new in the .NET Framework version 2.0. In earlier versions of the .NET Framework, an access violation in unmanaged code or unsafe managed code is represented by a NullReferenceException in managed code. A NullReferenceException is also thrown when a null reference is dereferenced in verifiable managed code, an occurrence that does not involve data corruption, and there is no way to distinguish between the two situations in versions 1.0 or 1.1.

Administrators can allow selected applications to revert to the behavior of the .NET Framework version 1.1. Place the following line in the <runtime> Element section of the configuration file for the application:

<legacyNullReferenceExceptionPolicy enabled = "1"/>
Inheritance Hierarchy

System..::.Object
  System..::.Exception
    System..::.SystemException
      System..::.AccessViolationException
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0
See Also

Reference

Other Resources

Tags :


Community Content

transwikiuser
HRESULT
AccessViolationException uses the HRESULT E_POINTER, which has the value 0x80004003. dddd
Tags : contentbug

Van den Driessche Willy
AccessViolation in a .NET program that uses legacy ActiveX controls

When you receive an AccessViolation exception in an application that uses legacy ActiveX controls on Windows XP SP3 or Windows Vistaor higher (after a fresh compilation) then this might be related to an upgrade in the microsoft compilers . These compilers now mark the generated EXE with the NXCOMPAT flag. This flag is part of the EXE header. (You can inspect it with DumpBin.exe). It basically says "this executable is safe for DEP (Data Execution Prevention)"

This fact is documented in

http://blogs.msdn.com/ed_maurer/archive/2007/12/14/nxcompat-and-the-c-compiler.aspx



You can solve this by adding a post-build event to switch this flag back off (using EditBin.exe)

I personally find this a bug as reported here:

https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=526540


It cost us two weeks with 2 programmers to find this "gem", which is why I document it here.

A specialized exception would have prevented us all the trouble.

Tags : nxcompat

Page view tracker