For Loop count being reduced
If a ‘For’ loop uses the number of items on a list (such as a TList, TStringList, etc.) then a check is made to see if items from that list are removed.
For I:=0 To StrList.Count-1 do //where strlist is a TStringList
if State then
StrList.Delete(I); //this will cause an access violation
An attempt is made to access an item on the list that no longer exists. There are a number of possible solutions. Two of these might be to use a while statement or to use downto in the for loop and count in descending order in the loop.
Possible solution 1:
For I:=StrList.Count-1 downto 0 do //where strlist is a TStringList
if State then
StrList.Delete(I);
Possible solution 2:
I:=0;
while I<StrList.Count do begin//where strlist is a TStringList
if State then
StrList.Delete(I);
Inc(I);
end;
< Back to list of hints and warnings
|