If you have done it right, there should not be a problem.

In your example, when you create your delegation object - In your TMyOBject.Create you 
need to have a ._addref on your delegation object, ie TMyOBject holds a reference to 
your object.

When TMyObject is destroyed, in it's destructor you do a _release - actually with 
D3/D4 you assign nil to your reference which then calls _release for you, hence you 
delegated object is then destroyed when it's holder object is destroyed - or you 
specifically destroy the delegation object - be careful here with AV's. So the problem 
then boils down to a normal lifecycle maintenance of your holder object.

Your example X := TMyObject.Create; is not what I would recommend.

You could perhaps fudge it in that when ITesting calls _release - when it goes out of 
scope in your simple example, in TMyOBject._release you could check and see if it will 
cause the delegation object to be destroyed and then destroy yourself (Not very nice 
but can work).

Does that all make sense??

Myles.



-----Original Message-----
From:   Aaron Scott-Boddendijk [SMTP:[EMAIL PROTECTED]]
Sent:   Monday, April 12, 1999 11:27 AM
To:     Multiple recipients of list delphi
Subject:        [DUG]:  Interfaces Again ;)

I have a question regarding interface delegation and reference counting.

If a non interfaced object is extended with an interface and that interface uses
delegation to implement that interface then how is instance destruction detected
by the delegating class. Example below

if the following is done.
var
  X : ITesting;
begin
  X := TMyObject.Create;
end;

at the end of this the TTesting delegated object is destroyed but the Delegating object
(TmyObject) is not released.  Suggestions. Classes and interfaces below. I am looking
at how delegation can assist in aggregation but am unsure about management of
instances. 

type
  ITesting = interface
    function Test:String;
  end;

  TTesting = class(TInterfacedObject,ITesting)
  public
    function Test :String;
  end;

  TMyObject = class(TObject,ITesting)
  private
    FTesting :TTesting;
    property Tst:TTesting read FTesting implements ITesting;
  public
    constructor Create;
  end;

implementation

{ TTesting }

function TTesting.Test: String;
begin
  result := 'Tested';
end;

{ TMyObject }

constructor TMyObject.Create;
begin
  inherited;
  FTesting := TTesting.Create;
end;

--
Aaron Scott-Boddendijk
Jump Productions
(07) 838-3371 Voice
(07) 838-3372 Fax

---------------------------------------------------------------------------
    New Zealand Delphi Users group - Delphi List - [EMAIL PROTECTED]
                  Website: http://www.delphi.org.nz

application/ms-tnef

Reply via email to