Random access violation exceptions

John Mackey wrote:

> Hello everyone,

> I've had reports from various users about seemingly *random*
> Windows "Access violation" and "Invalid pointer" exceptions
> in runtime when running an application I built in Delphi 3.0.

> I do want to try to solve this myself rather than post code,
> so I would like to ask authors what you tend to immediately
> suspect when you hear such reports.

> The most obvious idea was to check all instances of TObject.Create
> to make sure they were properly freed and with no further
> references to TObject.  However, this doesn't appear to be
> causing the problem.  So I'm starting to run out of ideas.

Hmm... Okay. My top ten access violation generators. It's a fairly
spectacular list of {*word*77}oo's so this is gonna be a bit of a

1. Freeing an object and not setting all the pointers that point to it
to nil.

2. Doing 1 because some part of the program assumes "nil" means "this
part of the data structure no longer applicable", and another part
forgets this.

3. Dereferencing null pointers (seems to happen a lot less to me than

4. Miscalculating array indexes with index checking turned off.

5. {*word*99}ping all over your own stack by typecasting {untyped parameters
which were an array of pointers} to {other untyped pointers} and getting
the number of indirections wrong. (I never found that one... someone on
the group did the legwork for me!)

6. Trying to dynamically load a DLL and then forgetting to check the
return values to GetProcAddress before using them!

7. Accidentally treating strings as PChars and inadvertently trying to
memcpy them (yeah.. I know... just *don't ask*... it was some
marshalling code).
8. MapViewOfFile (nuff said).
9. Freeing something in it's own event handler.
10. Various other random acts of sheer stupidity.

> Also, has anyone heard of these kinds of exceptions being
> raised when a function within the application is DEFINITELY
> NOT called but merely contains bad code?


>  I had an experience
> like this with one application and needless to say it made
> debugging a headache.  I simply removed some pointer references
> in the uncalled function and the random errors went away.

You *sure* it was never called?

Martin Harvey.