SortedDictionary appears to not honor doOwnsValues

Issue #411 resolved
Mark Ford created an issue

Using:

Result := TCollections.CreateSortedDictionary<String, TMyObj>(TStringComparer.OrdinalIgnoreCase, [doOwnsValues]);

and the value objects are not freed.

While using:

  Result := TCollections.CreateDictionary<String, TMyObj>(TStringComparer.OrdinalIgnoreCase, [doOwnsValues]);

Does correctly free values.

Comments (5)

  1. Stefan Glienke repo owner

    I cannot repro - also you did not specify a version.

    In 2.0 and latest develop this prints ‘destroy’:

    type
      TMyObj = class
        destructor Destroy; override;
      end;
    
    destructor TMyObj.Destroy;
    begin
      Writeln('destroy');
      inherited;
    end;
    
    begin
      var dict := TCollections.CreateSortedDictionary<String, TObject>(TStringComparer.OrdinalIgnoreCase, [doOwnsValues]);
      dict.Add('foo', TMyObj.Create);
      dict.Remove('foo');
    end.
    

  2. Mark Ford reporter

    It does appear that removing items will destroy the objects. But if you don’t remove them and just let the dictionary go out of scope they don’t appear to clear.

    begin
      var dict := TCollections.CreateSortedDictionary<String, TObject>(TStringComparer.OrdinalIgnoreCase, [doOwnsValues]);
      dict.Add('foo', TMyObj.Create);
    end.
    

    This appears to report that TMyObj is leaked. Apologies about the version, I pulled the latest changes just in case it was fixed before I reported and wasn’t sure exactly what version to pick.

  3. Mark Ford reporter

    Also Dict.Clear doesn’t seem to free the objects (I assume that’s the same issue as going out of scope.)

  4. Log in to comment