Ad Space — Top Banner

EAccessViolation

Delphi Programming Language

Severity: Critical

What Does This Error Mean?

EAccessViolation means your program tried to read from or write to a memory address it is not allowed to access. The most common cause is a nil pointer — you are using an object variable that has not been created yet (or has already been freed). Windows detects the illegal memory access and raises this exception.

Affected Models

  • All Delphi versions
  • RAD Studio
  • Lazarus/Free Pascal (as EAccessViolation)

Common Causes

  • Using an object variable that is nil — it was declared but never created with a constructor
  • Using an object after it has been freed (using a dangling pointer)
  • Calling a method on an object whose parent has already been destroyed
  • An array or pointer variable pointing to an invalid or already-freed memory address
  • Accessing a COM interface pointer that was not properly initialized

How to Fix It

  1. Run the application in the Delphi debugger. When EAccessViolation fires, look at the call stack (View → Debug Windows → Call Stack). Find the first line of YOUR code in the stack.

    The debugger also shows the value of variables at the point of the crash, which usually reveals the nil pointer.

  2. Find the object variable mentioned in the crashing line. Check where it is created (the constructor call). If it is created conditionally or in an event handler, it might not have been created yet.

    Look for a line like: MyObject := TMyClass.Create; — if this line has not run yet, the variable is nil.

  3. Add a nil check before using the object: if Assigned(MyObject) then MyObject.DoSomething. The Assigned() function checks if a pointer or object reference is not nil.

    Assigned() is the correct Delphi way to check for nil — it works for objects, pointers, and event handlers.

  4. If you free an object and then use it, set the variable to nil after freeing: FreeAndNil(MyObject) does both in one step and is safer than just calling Free.

    After FreeAndNil, the variable is nil. Any subsequent Assigned() check will return False and prevent the access violation.

  5. Enable FastMM (included in modern Delphi) in full debug mode. It detects use-after-free errors and reports exactly which object was accessed after being freed.

    Add FastMM4 as the first unit in the DPR file. In debug mode it provides detailed error reports.

When to Call a Professional

EAccessViolation bugs can be tricky to track down but are always fixable. Enable the debugger and look at the call stack when the exception fires to find the nil object. If the bug is intermittent and hard to reproduce, a memory debugging tool like FastMM or MadExcept can help pinpoint the source.

Frequently Asked Questions

What does 'read of address 00000000' mean?

It means your code tried to read from memory address zero, which is nil. This always means you are using an object or pointer variable that was never initialized. Find the variable that is nil and make sure it is created before use.

Why does the same code sometimes crash and sometimes not?

Memory that has been freed is not immediately cleared to zero. Sometimes the freed memory still contains valid-looking data, so the object appears to work. Other times the memory has been reused for something else, and accessing it causes an immediate crash. This is why use-after-free bugs are intermittent — the behavior depends on what else the program has done with memory.

What is the difference between EAccessViolation and EInvalidPointer?

EAccessViolation means the CPU denied a memory access — typically nil dereference or accessing freed memory. EInvalidPointer means you tried to free a pointer that the memory manager does not recognize — typically freeing the same object twice or passing a non-heap pointer to Free. Both indicate memory management bugs.