Thread View: alt.comp.lang.borland-delphi
3 messages
3 total messages
Started by pete
Thu, 20 Mar 2003 23:42
occasional 'list index out of bounds error'
Author: pete
Date: Thu, 20 Mar 2003 23:42
Date: Thu, 20 Mar 2003 23:42
126 lines
5296 bytes
5296 bytes
Hi
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;
Re: occasional 'list index out of bounds error'
Author: Nicholas Sherlock
Date: Fri, 21 Mar 2003 03:26
Date: Fri, 21 Mar 2003 03:26
18 lines
2078 bytes
2078 bytes
"pete" <deepthinker@btinternet.com> wrote in message news:b5djka$1p1$1@titan.btinternet.com... > Hi > > 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? <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
Re: occasional 'list index out of bounds error'
Author: Nis Jorgensen
Date: Fri, 21 Mar 2003 09:12
Date: Fri, 21 Mar 2003 09:12
28 lines
2417 bytes
2417 bytes
On Fri, 21 Mar 2003 15:26:37 +1200, "Nicholas Sherlock" <n_sherlock@hotmail.com> wrote: >"pete" <deepthinker@btinternet.com> wrote in message >news:b5djka$1p1$1@titan.btinternet.com... >> Hi >> >> 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? > ><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? 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads