Как забыть о необходимости разрушать объекты?

Previous  Top  Next

    
 

 

 

Code:

type

ISelfDestroy = interface;

//forget about GUID, if you are not using COM

 

TSelfDestroy = class(TInterfacedObject, ISelfDestroy)

private

   FObject: TObject;

public

   constructor Create(AObject: TObject);

   destructor Destroy; override;

end;

 

implementation

 

constructor TSelfDestroy.Create(AObject: TObject);

begin

FObject := AObject;

end;

 

destructor TSelfDestroy.Destroy;

begin

FreeAndNil(FObject);

inherited;

end;

 

 

// So when you use, just do like this...

 

procedure TForm1.Button1Click(Sender: TObject);

var

MyObject: TMyObject;

SelfDestroy: TSelfDestroy;

begin

MyObject    := TMyObject.Create;

SelfDestroy := TSelfDestroy.Create(MyObject);

// The MyObject will free automatically as soon as TSelfDestroy

// goes out of scope.

// Carry on your code here...

end;

 

 

©Drkb::00122

Взято с сайта http://www.swissdelphicenter.ch/en/tipsindex.php