It appears to me that IList can NOT take Event handler as its element. The program has access violation $C00000005 on PROGRAM exit.
Everything is fine if I use Delphi RTL's TList.
The access violation happens for both 32 bit and 64bit build. When it happens, it seems to halt at the following lines of Spring4D:
procedure TCollectionBase<T>.Changed(const item: T; action:
TCollectionChangedAction);
begin
if fOnChanged.CanInvoke then
fOnChanged.Invoke(Self, item, action);
end;
The following sample program can replicate the access violation, using RAD Studio Tokyo 10.2.3, on Windows.
program Test_Spring_IList_With_Event_Handler;
{$APPTYPE CONSOLE}
{$R *.res}
uses
System.SysUtils,
Spring.Collections;
type
TSomeEvent = procedure of object;
TMyEventHandlerClass = class
procedure SomeProcedure;
end;
TMyClass = class
private
FEventList: IList<TSomeEvent>;
public
constructor Create;
destructor Destroy; override;
procedure AddEvent(aEvent: TSomeEvent);
end;
procedure TMyEventHandlerClass.SomeProcedure;
begin
// Nothing to do.
end;
constructor TMyClass.Create;
begin
inherited;
FEventList := TCollections.CreateList<TSomeEvent>;
end;
destructor TMyClass.Destroy;
begin
FEventList := nil;
inherited;
end;
procedure TMyClass.AddEvent(aEvent: TSomeEvent);
begin
FEventList.Add(aEvent);
end;
var
MyEventHandlerObj: TMyEventHandlerClass;
MyObj: TMyClass;
begin
MyObj := TMyClass.Create;
MyEventHandlerObj := TMyEventHandlerClass.Create;
try
MyObj.AddEvent(MyEventHandlerObj.SomeProcedure);
finally
MyObj.Free;
MyEventHandlerObj.Free;
end;
end.