Delphi-PRAXiS
Seite 2 von 2     12   

Delphi-PRAXiS (https://www.delphipraxis.net/forum.php)
-   Win32/Win64 API (native code) (https://www.delphipraxis.net/17-win32-win64-api-native-code/)
-   -   Delphi COM: TInterfacedObject-Nachfahren explizit freigeben (https://www.delphipraxis.net/1042-com-tinterfacedobject-nachfahren-explizit-freigeben.html)

sakura 14. Okt 2002 11:31

Die Objekte, welche intern erstellt werden, können direkt über den Klassenbezeichner und dessen Constructor erstellt werden. Delphi handelt das COM Interface automatisch. Anschließend wird intern eine Referenz auf das Interface gesetzt und eine nach aussen gegeben. RefCount = 2...

Bsp:
Code:
type
  TIntfClass = class(TComObject);
  private
    FMyRefToAnotherIntf: IAnotherIntf;
  ...
  protected
    function Get_AnotherIntf: IAnotherIntf; stdcall;
    ...
  end;
...
function TCOMServer.Get_AnotherIntf: IAnotherIntf;
begin
  FMyRefToAnotherIntf := TAnotherIntf.Create(...);
  Result := FMyRefToAnotherIntf;
end;

Udontknow 14. Okt 2002 13:17

Ich poste vielleicht einfach mal ein bisschen Quellcode, um das Problem zu erläutern.

Ich habe eine Grund-Klasse, die im wesentlichen einfach nur das übergeordnete Objekt als Eigenschaft einführt.
Dann habe ich noch eine Master- und eine Detailklasse. Der Master enthält ein untergeordnetes Detailobjekt, das einen einfachen WideString als Eigenschaft besitzt.

Code:
type TXNComponent=class(TInterfacedObject, IXNComponent)
  private
    FOWner:IXNComponent;
  public
    function GetOwner:IXNComponent; stdcall;
    constructor Create(AOwner:IXNComponent); virtual;
    destructor Destroy; override;
    function XNObjectName:WideString; stdcall;
end;

type TXNDetail=class(TXNComponent,IXNDetail)
  private
    FStr:WideString;
  public
    function GetStr:WideString; stdcall;
end;

type TXNMaster=class(TXNComponent,IXNMaster)
  private
    FDetail:IXNDetail;
  public
    constructor Create(AOwner:IXNComponent); override;
    destructor Destroy; override;
    function GetDetail:IXNDetail; stdcall;
end;

implementation

{ TXNComponent }

constructor TXNComponent.Create(AOwner:IXNComponent);
begin
  inherited Create;
  ShowMessage(XNObjectName+'.Create');
  FOwner:=AOwner;
end;

destructor TXNComponent.Destroy;
begin
  ShowMessage(XNObjectName+'.Destroy');
  FOwner:=NIL;
  inherited;
end;

function TXNComponent.GetOwner: IXNComponent;
begin
  Result:=FOwner;
end;

function TXNComponent.XNObjectName: WideString;
begin
  Result:='';
  if FOwner<>NIL then
  Result:=FOwner.XNObjectName+'\';
  Result:=Result+Self.ClassName;
end;

{ TXNDetail }

function TXNDetail.GetStr:WideString;
begin
  Result:=FStr;
end;

{ TXNMaster }

constructor TXNMaster.Create(AOwner:IXNComponent);
begin
  inherited;
  FDetail:=TXNDetail.Create(Self);
end;

destructor TXNMaster.Destroy;
begin
  FDetail:=NIL;
  inherited;
end;

function TXNMaster.GetDetail: IXNDetail;
begin
  Result:=FDetail;
end;
Die Interfaces dazu:

Code:
type
  IXNComponent=interface(IUnknown)
    ['{F0BB7952-1B45-42C7-9533-63B46AC10D9C}']
    function XNObjectName:WideString; stdcall;
    function GetOwner:IXNComponent; stdcall;
    property Owner:IXNComponent read GetOwner;
end;

  IXNDetail=interface(IXNComponent)
    ['{E0837319-EEE7-4AFC-B199-27F7C7D18373}']
    function GetStr:WideString; stdcall;
    property Str:WideString read GetStr;
end;

  IXNMaster=interface(IXNComponent)
    ['{EA3940CD-0F38-4390-ADDF-4A034E5C6CB2}']
    function GetDetail:IXNDetail; stdcall;
    property Detail:IXNDetail read GetDetail;
end;
So, wenn du nun diese Struktur erstellst ...

Code:
var Master:IXNMaster;
begin
  //Master erstellen
  Master:=TXNMaster.Create(NIL);
  //Master freigeben
  Master:=NIL;
end;
...wirst du sie nicht so einfach wieder verwerfen können. Ich hatte auch schon eine Routine TXNComponent.DoDestroy, die einfach mal ein Free gemacht hat. Das brachte eben, wie schon gesagt, Exceptions...

Cu,
Udontknow


Alle Zeitangaben in WEZ +1. Es ist jetzt 05:07 Uhr.
Seite 2 von 2     12   

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
LinkBacks Enabled by vBSEO © 2011, Crawlability, Inc.
Delphi-PRAXiS (c) 2002 - 2023 by Daniel R. Wolf, 2024-2025 by Thomas Breitkreuz