TList<T> deallocates if you use "For X in ..."

Issue #325 invalid
Jens Hykkelbjerg created an issue

The list enumerator will keep a reference to the list while looping through the list, but if the list is not already referenced through an interface, once the enumerator is deallocated, the entire list will also deallocate.

Sample code that reproduces behaiviour:

uses
  System.SysUtils,
  Spring.Collections.Lists;

var
  vList: TList<Integer>;
  i,j: integer;

  procedure DeAllocatesList(AList: TList<Integer>);
  var
    i: Integer;
  begin
    for i in AList do begin
      // Do nothing ... or something, doesn't matter, the list is destroyed
    end;
  end;
begin
  vList :=  TList<Integer>.Create;
  DeAllocatesList(vList);
  vList.Add(1); // This statement fails
end.

Perhaps the enumerator could check if the list was already referenced before increasing refcount

Comments (2)

  1. Log in to comment