Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delphi access violation assigning local variable

This seems like the simplest thing in the world and I'm ready to pull my hair out over it.

I have a unit that looks like this ;

Unit  myUnit;
// ...
//normal declarations
//...
Public
//bunch of procedures including
Procedure myProcedure;

const
//bunch of constants

var
//bunch of vars including
myCounter:integer;

Implementation
Uses //(all my uses)

// All of my procedures including

Procedure myProcedure;
   try
     // load items from file to TListBox - this all works
   except
   on EReadError do begin
     // handle exception
     end;
   end; //try



   myCounter:=0;  //  <--  ACCESS VIOLATION HERE
   while myCounter //...etc

It's a simple assignment of a variable and I have no idea why it is doing this. I've tried declaring the variable local to the unit, to the procedure, globally - no matter where I try to do it I can't assign a value of zero to an integer, declared anywhere, within this procedure without it throwing an access violation. I'm totally stumped.

I'm calling the procedure from inside a button OnClick handler from within the same unit, but no matter where I call it from it throws the exception. The crazy thing is that I do the exact same thing in a dozen other places in units all over the program without problems. Why here? I'm at a total loss.

like image 729
J... Avatar asked Apr 27 '10 17:04

J...


4 Answers

Read of address 00000008 means that you're reading a variable at an offset of 8 bytes from a nil pointer. That doesn't fit what you're trying to do here at all, since you're writing, not reading, and you're writing a constant, not a variable read from somewhere.

Are you sure that this is the actual line that's triggering the exception? Have you put a breakpoint on this line? Have you tried moving this line to the top of the procedure?

It's hard to be sure without your actual code in front of me, but if I had to guess, I'd say that the line before this is causing the exception, and then the instruction pointer has already incremented so Delphi highlights the next line.

like image 79
Mason Wheeler Avatar answered Nov 04 '22 03:11

Mason Wheeler


I don't think your error means anything like what it appears to. When you get an access violation from a piece of code like this that has no sane way of producing an access violation you're looking at trashed memory in some fashion.

Step through the offending code in the CPU window and see what's really happening.

like image 24
Loren Pechtel Avatar answered Nov 04 '22 04:11

Loren Pechtel


You are using with statements and are looking at a different myCounter?

Some part of your code is writing in memory it shouldn't or freeing while it shouldn't and that by accident it results in AV's when accessing that specific local variable?

like image 1
Lars Truijens Avatar answered Nov 04 '22 03:11

Lars Truijens


The assignment myCounter := 0 throwing an access violation suggests that either the data segment that the global vars are stored in has been removed from memory, or that the registers are hosed in your myProcedure routine.

Use the CPU view to see what registers are used to access the global var, and then work backward from there to see where that/those registers go awry.

like image 1
dthorpe Avatar answered Nov 04 '22 04:11

dthorpe