update: apparently delphi is supposed to do it this way, which imho is one of the stupidest things ever thought of
Borland has a nasty compiler optimization in Delphi 2006 which caused a nice little bug in my app today.
Apparently the conditional of a for loop gets cached which can be a bad thing, such as in the following case using a TObjectList:
for i := 0 to SomeObjectList.Count - 1 do
begin
// Some conditional causes a given object i to be removed which
// should update SomeObjectList.Count, but the original value of
// SomeObjectList.Count seems to be cached (probably in a register)
// meaning that further iterations through the loop test against the
// original value of SomeObjectList.Count instead of being updated
// every iteration of the loop. Bam, instant array index out of bound.
end;
Funny thing, in the debugger, inspecting the variables "appear" to work correctly. It's funny (or not) to see the loop actually test, then execute when i = SomeObjectList.Count which it should NEVER do.
Ironically, the only way around this "feature" was for me to use a while loop and manually emulate a for loop with it.
(PS - TallBill, exactly like what you did originally, except in my case the compiler screwed up!)
Borland has a nasty compiler optimization in Delphi 2006 which caused a nice little bug in my app today.
Apparently the conditional of a for loop gets cached which can be a bad thing, such as in the following case using a TObjectList:
for i := 0 to SomeObjectList.Count - 1 do
begin
// Some conditional causes a given object i to be removed which
// should update SomeObjectList.Count, but the original value of
// SomeObjectList.Count seems to be cached (probably in a register)
// meaning that further iterations through the loop test against the
// original value of SomeObjectList.Count instead of being updated
// every iteration of the loop. Bam, instant array index out of bound.
end;
Funny thing, in the debugger, inspecting the variables "appear" to work correctly. It's funny (or not) to see the loop actually test, then execute when i = SomeObjectList.Count which it should NEVER do.
Ironically, the only way around this "feature" was for me to use a while loop and manually emulate a for loop with it.
(PS - TallBill, exactly like what you did originally, except in my case the compiler screwed up!)
