Examples Delphi

If there is any possibility of the freeing code being called re-entrantly, make sure to store a nil in the list before freeing the object; your code should look like this:

// original from Ray Lischner
for i := 0 to List.Count-1 do
begin
TempNode := List[i];
List[i] := nil;
TempNode.Free;
end;
List.Free;

Otherwise, the list would contain an invalid pointer. Even if you are not using multi-threading, Delphi has a few places where it is re-entrant.