Einzelnen Beitrag anzeigen

Benutzerbild von himitsu
himitsu

Registriert seit: 11. Okt 2003
Ort: Elbflorenz
43.142 Beiträge
 
Delphi 12 Athens
 
#4

AW: Memory Leak bei Interface Delegation

  Alt 27. Okt 2016, 09:44
Joar, hatte irgendwie das procedure als project gelesen.

Aaaaaaaaalso, im XE das Selbe.


Es liegt am Implements.
Aus irgendeinem Grund besitzt TMyWrapper zwei Referenzen, anstatt nur Einer.

Und da es nur eine Variable und auch keine "versteckte" Tempvariable gibt, steht RefCount am Ende (nach __Release im end; ) immernoch über 0, womit die Isntanz natürlich nicht freigegeben wird.

So property MyInterface: IMyInterface read FMyInterface ;//implements IMyInterface; gibt es kein Leck.

PS: Du hast vergessen der innere Interface zu erstellen.
So knallt das wunderschön, wenn man auf irgendwas von IInterface oder IMyInterface zugreifen will, da diese Referenz NIL ist.


Delphi-Quellcode:
type
  IMyInterface = interface
    procedure ShowRefCount;
  end;

  TMyClass = class(TInterfacedObject, IMyInterface)
    procedure ShowRefCount;
  end;

  TMyWrapper = class(TInterfacedObject, IMyInterface)
    FMyInterface: IMyInterface;
    constructor Create;
    property MyInterface: IMyInterface read FMyInterface implements IMyInterface;
  end;

procedure TMyClass.ShowRefCount;
begin
  ShowMessage(IntToStr(RefCount));
end;

constructor TMyWrapper.Create;
begin
  inherited;
  FMyInterface := TMyClass.Create;
end;

procedure TForm4.FormCreate(Sender: TObject);
var
  MyInterface: IMyInterface;
begin
  ReportMemoryLeaksOnShutdown := True;

  MyInterface := TMyWrapper.Create(); // <- mit dieser Zeile Memory Leak
  //MyInterface := TMyClass.Create(); // <- mit dieser Zeile KEIN Memory Leak

  //ShowMessage(IntToStr((MyInterface as TInterfacedObject).RefCount));
  MyInterface.ShowRefCount;
end;
Garbage Collector ... Delphianer erzeugen keinen Müll, also brauchen sie auch keinen Müllsucher.
my Delphi wish list : BugReports/FeatureRequests
  Mit Zitat antworten Zitat