Friday, May 06, 2005

Circular reference issue of IInterface Object: Delphi implementation

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;

type
IParent = interface(IInterface)
['{0097DA7B-2A00-48EC-9E40-80FA7D443DD4}']
end;

IChild = interface(IInterface)
['{06284805-8F15-4221-B1FF-C43B872C4A14}']
function GetParent: IParent;
property Parent: IParent read GetParent;
end;

TChild = class(TInterfacedObject, IChild)
private
FOuter: pointer;
function GetParent: IParent;
public
constructor Create(const AOuter: IParent);
procedure BeforeDestruction; override;
end;

TParent = class(TInterfacedObject, IParent)
private
FChild: IChild;
public
procedure AfterConstruction; override;
end;

TForm1 = class(TForm)
private
FMyParent: TParent;
public
procedure AfterConstruction; override;
procedure BeforeDestruction; override;
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.AfterConstruction;
begin
inherited;
FMyParent := TParent.Create;
end;

procedure TForm1.BeforeDestruction;
begin
inherited;
FMyParent.Free;
end;

procedure TChild.BeforeDestruction;
begin
inherited;
ShowMessage('Child Free');
end;

constructor TChild.Create(const AOuter: IParent);
begin
inherited Create;
FOuter := pointer(AOuter);
end;

function TChild.GetParent: IParent;
begin
Result := IParent(FOuter);
end;

procedure TParent.AfterConstruction;
begin
inherited AfterConstruction;
FChild := TChild.Create(Self);
end;

end.

3 Comments:

Blogger lewiswilliams3489 said...

I read over your blog, and i found it inquisitive, you may find My Blog interesting. My blog is just about my day to day life, as a park ranger. So please Click Here To Read My Blog

6:42 AM  
Blogger johnharolds8234290001 said...

I read over your blog, and i found it inquisitive, you may find My Blog interesting. So please Click Here To Read My Blog

http://pennystockinvestment.blogspot.com

9:43 AM  
Blogger joewarner53383548 said...

Get any Desired College Degree, In less then 2 weeks.

Call this number now 24 hours a day 7 days a week (413) 208-3069

Get these Degrees NOW!!!

"BA", "BSc", "MA", "MSc", "MBA", "PHD",

Get everything within 2 weeks.
100% verifiable, this is a real deal

Act now you owe it to your future.

(413) 208-3069 call now 24 hours a day, 7 days a week.

2:14 AM  

Post a Comment

<< Home