trying to enumerate recursively Keys or Values causes an EInvalidOperationException with the message 'Collection was modified; enumeration operation may not execute'

Issue #384 resolved
Mirko Bianco created an issue

Example code that reproduces the issue: 

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils,

Spring.Collections;

var
  Value1: string;
  Value2: string;
begin
  try
    var D := TCollections.CreateSortedDictionary<Integer, string>;

for var Index := 0 to 10 do
      D[Index] := IntToStr(Index);

for Value1 in D.Values do
      for Value2 in D.Values do
        Writeln(Value1, Value2);

Readln;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end.

A solution would be to create the IList on the fly when needed instead of recycling fKeys and fValues.

A workaround is to iterate the dictionary itself and work with the pairs.

Comments (2)

  1. Stefan Glienke repo owner

    I cannot reproduce this error with 2.0 (develop) - the sorted dictionary was completely reworked since 1.x

  2. Log in to comment