I have created a TMyList
-class that inherited from the Delphi TObjectList<T>
. After changing it to a TObjectList<T>
from Spring4D, I have an issue using the enumerator.
I we use the enumerator somewhere in the code, the object will be destroyed as soon as the enumerator is destroyed (eg. after for ... in ...
). This is due to the reference counting used inside the enumerator, but from the outside it is used as an object in the code and I expect it not to be auto destroyed.
This looks like a bug in Spring4D, or maybe it is simply not possible to use Spring4D lists with object references?
A simple example to reproduct is below:
unit Unit188;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Spring.Collections.Lists, Vcl.StdCtrls;
type
TForm188 = class(TForm)
Button1: TButton;
procedure Button1Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;
TMyList = class(TObjectList<TObject>)
procedure DoSomething;
destructor Destroy; override;
end;
var
Form188: TForm188;
implementation
{$R *.dfm}
{ TMyList }
destructor TMyList.Destroy;
begin
ShowMessage('Destructor called');
inherited;
end;
procedure TMyList.DoSomething;
var
o: TObject;
begin
for o in Self do; // <=== here the TMyList is destroyed as soon as the enumerator is destroyed
end;
procedure TForm188.Button1Click(Sender: TObject);
var
l: TMyList;
begin
l := TMyList.Create;
l.DoSomething;
end;
end.