unit Unit3;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, Contnrs;
type
IMyInterface =
Interface
['
{6D3FE306-CB00-44CC-A8B1-31BF6F06127A}']
function test:
String;
End;
TmyObject =
class (TInterfacedObject, IMyInterface)
private
FDatum:
String;
public
function test:
String;
constructor Create(AString: TDate);
destructor destroy;
end;
TForm3 =
class(TForm)
Button1: TButton;
Label1: TLabel;
Button2: TButton;
procedure Button1Click(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
AList: TObjectList;
public
end;
var
Form3: TForm3;
implementation
{$R *.dfm}
procedure TForm3.Button1Click(Sender: TObject);
var
lItem: IMyInterface;
begin
AList.Add(TmyObject.Create(Now));
if Supports(AList.Items[0], IMyInterface, lItem)
then // <-- Lasse ich diese
Label1.Caption := lItem.test;
// zwei Zeilen weg, läufts
end;
{ TmyObject }
constructor TmyObject.Create(AString: TDate);
begin
FDatum := dateToStr(AString);
end;
destructor TmyObject.destroy;
begin
ShowMessage('
Free!');
end;
function TmyObject.test:
String;
begin
Result := FDatum;
end;
procedure TForm3.Button2Click(Sender: TObject);
var
i: Integer;
begin
ShowMessage('
Free: ' + IntToStr(AList.Count));
FreeAndNil(AList);
// <-- Hier bekomme ich: "Zugriffsverletzung bei Adresse 0067000B. Schreiben von Adresse F0000001."
end;
procedure TForm3.FormCreate(Sender: TObject);
begin
AList := TObjectList.Create;
end;
end.