Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

EAccessViolation

Status
Not open for further replies.

bearsite4

Programmer
Sep 29, 2001
79
AU
Hi,

Nice to see you here Svanels- a familiar 'face' as it were.

I'm writing a simple program at the moment and I'm getting annoying EAccessViolation's that I don't know how to fix.
This occurs when I'm trying to read or write to one of my class variables.

Now here's a skeleton of my code:

type
TBaroomForm = class(TForm)
Button1: TBitBtn;
.
.

procedure Button1Click(Sender: TObject);
.
.

private
programs: array [ 1..nButtons-1 ] of String;

procedure readConfigEntries;
.
.

end;

Now these are the lines that cause the violation:

readln( configF, programs[n] );
length( programs[n] );

Apart from this, I tried assigning values directly to programs[index] and this also cause a violation.


First of all, I though that it might be because I declared the variable private however if I couldn't access it for this reason I'd thought the compiler wouldn't even compile it. So I don't think that's the reason.

As I mentioned above length( program[n] ) causes the violation but when I evaluate/modify it I get the correct answer. Also when I modify th string for programs[n] it appears updated but when I evaulet programs[n] again the string is back to the original.

Can anyone help me? I am completely clueless.


As a side question: I have to read in settings from a config file before the main form runs. I've put it in the initialisation part of the unit. Is this a good idea or are there more elegant ways?

 
Just a guess, but since you're starting at one you probably want your array to go from 1 to nButtons, not nButtons-1. You're probably trying to access 1 past the end of your array... TealWren
 
Nah, I have nButtons but I reserved one of them for a special task so I don't assigned a program to it. That's why I have nButtons-1. I'm pretty sure my array is set up properly since when I use the evaluate/modify it's as I expected.
 
I suppose nbuttons is related to the number of buttons on your form? If it is so then the form must be created first before you can use n.
Try it with hardcoding nbuttons first.
Or am I wrong? S. van Els
SAvanEls@cq-link.sr
 
The reason I guessed that is that I don't see anything in the code there that would cause an access violation. Have you assigned and reset your file? Have you stepped through to see if it happens on the first iteration or later? TealWren
 
I think you might be right Svanels. Yopu know, I even thought of this but when stepping with the debugger I got the (false) impression that the forms were created before my config file was read.

So where should I put my code to read the config file then. I could put it in the main project after the forms are created but doesn't Delphi state that you will rarely edit the main project src?

Hopefully, this is the source of the problem and I can get my program working. Thanks to everyone who put in a hand.
 
Take a sufficient big number, call it maxButtons, or create a TStringlist dynamically which is a more elegant solution.

Would be sometthing like this:

var Programs : TStringList;


In the formCreate event type in:

Programs:= TStringList.Create;

Fill it up:
Programs.Add('something');

Then you could use Programs[n]



S. van Els
SAvanEls@cq-link.sr
 
Welcome to the Delphi Forum Bearsite4
I dont understand what you mean by the main source.
Delphi dosnt have a 'main' procedure/function like standard Pascal or (ugg) 'C'.

Steve..
 
Thanks Steve,

When I said main source I was referring to the project file that Delphi creates.

But not to worry as I have found the source of my problems. My first Delphi program complete! (albeit a simple one). Woohoo!!
 
Well bearsite4 what was the cause? S. van Els
SAvanEls@cq-link.sr
 
It was exactly as you said. I hadn't created the form before I tried to access it's programs array.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top