TObjectDataSet cannot delete final record if modified

Issue #398 invalid
Josh Gardner created an issue

Reproduced on 1.2.6 and develop/346a1f5a

The implementation of TObjectDataSet has an issue when deleting modified records.

When removing the final record from a TObjectDataSet, if the dataset is in a modified state it will cause an index out-of-bounds exception. From what I can tell, whenever there is a modified record being deleted the IndexList becomes out of sync when performing the final DoPostRecord.

This is a small test project to reproduce the issue:

program ODSModifiedRemove;

{$APPTYPE CONSOLE}

{$R *.res}

uses
  System.SysUtils, Spring.Data.ObjectDataSet, Spring.Collections;

type
  {$M+}
  TFoo = class
  private
    FStr: String;
  published
    property Str: String read FStr write FStr;
  end;

begin
  try
    var ODS := TObjectDataSet.Create(nil);
    ODS.DataList := TCollections.CreateList<TFoo>(True) as IObjectList;
    ODS.Active := True;
    var Foo := TFoo.Create;
    Foo.Str := 'asdf';
    ODS.DataList.Add(Foo);
    ODS.Refresh;
    ODS.TrackChanges := True;

    ODS.Edit;
    ODS.FieldByName('Str').Value := 'fdas';

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

Comments (1)

  1. Stefan Glienke repo owner

    It’s simple: don’t mix dataset and list operations - if you want to remove the record you are at, then call Delete on the dataset.

  2. Log in to comment