I have an application that occasionally (about 10% of the time) generates
the error 'list index out of bounds' on execution, but I have yet to see the
error while running the project via the debugger. I have listed the code
below. Can anyone see any possible problems with the code?
Basically, the application gets the username of the person logged on from
windows and then queries an MS Access database using this data to see if a
persons details exist. Because of problems with >30 users accessing the
database simultanioulsy (very slow network at times) The database is queried
in a randomly generated timer procedure.
any help appreciated.
code:
// get the name of the station, in the form normally in the form T11-20
where T11 is room and 20 is staion number
function getStationName:string;
var
stationName : string;
stationNameLen : Dword;
begin
stationNameLen := 255;
SetLength(stationName, stationNameLen);
If GetComputerName(PChar(stationName), stationNameLen) Then
Result := Copy(stationName,1,stationNameLen - 1)
Else
Result := 'Unknown';
End;
// get the the username of person logged on. normally digit,digit, then 6
char
Function GetUserFromWindows: string;
Var
UserName : string;
UserNameLen : Dword;
Begin
form1.Text:='getting username';
UserNameLen := 255;
SetLength(userName, UserNameLen);
If GetUserName(PChar(UserName), UserNameLen) Then
Result := Copy(UserName,1,UserNameLen - 1)
Else
Result := 'Unknown';
End;
// check if there are details in MS Access database for this user
function tform1.UserInDatabase:boolean;
var
tryagain:boolean;
begin
form1.Text:='checking for user in database';
{search for an entry in students table that matches the username}
ADOQuery.Close;
with ADOQuery do
begin
sql.clear;
sql.add('select * from students where userName =?');
Parameters[0].value := user;
end;
tryagain:=true;
while tryagain do
begin
tryagain:=false;
try
ADOQuery.Open;
except
begin
messageDlg('Unable to open query',mterror,[mbok],0);
tryagain:=true; {unable to open query so try again}
end;
end;
if (ADOQuery.fieldbyname('forename').asstring='') then
begin
messageDlg('Call your teacher, details for '+user+' do not
exist',mterror,[mbok],0);
UserInDatabase:=false;
end
else UserInDatabase:=true;
end;
end;
procedure TForm1.FormCreate(Sender: TObject);
var
name:string;
time:integer;
stationName:string;
dummy:integer;
begin
firstTime:=true;
form1.Text:='activating UDP';
idUDPServer1.Active := True;
form1.Text:='get username';
user:=GetUserFromWindows;
form1.Text:=user;
stationName:=getStationName;
room:=stationName[1]+stationName[2]+stationName[3];
Randomize;
file://prevent timer interval of 0
repeat
time:= Random(200);
until time>0;
time:=time*100;
form1.Text:='timer int:'+inttostr(time);
timer1.interval:=time;
timer1.Enabled:=true;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
var
name:string;
begin
timer1.enabled:=false;
if UserInDatabase then
begin
form1.Text:='user in database';
name:=ADOQuery.fieldbyname('forename').asstring+ '
'+ADOQuery.fieldbyname('surname').asstring;
form1.Text:=name;
end;
end;
<Snip code>
Well I think that the only code that could generate such an error in this
example would be the stationname[0], stationname[1], stationname[2],
stationname[3] references. How is this array created? Populated? Free'd?
Cheers,
Nicholas Sherlock
It's a string, accessed as an array of chars.
I guess the error is the assumption that Length(stationName) > 2.
--
Nis Jørgensen
Amsterdam
Please include only relevant quotes, and reply below the quoted text. Thanks