Closed vveliu-csw closed 1 year ago
For deleting items from lists, I always count down from the last element to zero so the index is always valid. Saves you the boolean for the repeat termination.
Like (untested from the top of my head) this:
for i := FLoggerThread.Appenders.Count -1 downto 0 do
if Self.FLoggerThread.FAppendersDecorators[i].FLogAppender = aAppender then
begin
Self.FLoggerThread.FAppendersDecorators.Delete(i);
break;
end;
@luebbe Edit: I get it! Your version of the loop allways evaluates the Count property so in case of delete the decrease of count prevent the index to go out of range.
It's true that you can break the loop after the delete but I think for
is intended to loop trought the entire range. repeat until
on the other hand can stop at some point. This is my personal opinion!
Deleting appender at runtime, raises
Argument Out Of Range Exception
.The problem is in the
DelAppender
procedure:The
for
statement loops tilli = Self.FLoggerThread.FAppendersDecorators.Count - 1
, this is a mistake beacuse if we delete some Appender contained in the list the higher limit of the loop is not re assigned. In a list of 3 itemsfor
will loop from 0 to 2 but if we delete an item the loop don't stop so trying to read/writeList[2]
will raise the exception.I used a
repeat .. until ..
statement instead, the loop should stop after succesfull delete (appender is supposed to exists only once in the list).Modified version of mine:
Plus. I removed the above code beacuse
Self.FLogAppenders and Self.FLoggerThread.FAppenders
are the same list: