Hier noch ein paar Infos:
AggregatedObject
Stackoverflow
Delphi-Quellcode:
unit Unit1;
interface
type
IMyInterface =
interface
procedure ShowInfo;
end;
// Ableiten von TAggregatedObject
TMyClass =
class(TAggregatedObject, IMyInterface)
procedure ShowInfo;
end;
TMyWrapper =
class(TInterfacedObject, IMyInterface)
private
// Wir "besitzen" also die Klasse selber speichern
FMyClass: TMyClass;
function getMyInterface: IMyInterface;
public
constructor Create;
destructor Destroy;
override;
property MyInterface: IMyInterface
read getMyInterface
implements IMyInterface;
end;
procedure Test;
implementation
uses
System.SysUtils;
procedure TMyClass.ShowInfo;
begin
writeln('
Call ShowInfo');
end;
constructor TMyWrapper.Create;
begin
inherited;
FMyClass := TMyClass.Create(self);
end;
destructor TMyWrapper.Destroy;
begin
inherited;
// Klasse freigeben
FMyClass.Free;
end;
function TMyWrapper.getMyInterface: IMyInterface;
begin
result := FMyClass
as IMyInterface;
end;
procedure Test;
var MyInterface: IMyInterface;
begin
MyInterface := TMyWrapper.Create();
// <- mit dieser Zeile Memory Leak
MyInterface.ShowInfo;
// Kein Leak :-)
end;
end.