Articles   Members Online: 3
-Article/Tip Search
-News Group Search over 21 Million news group articles.
-Delphi/Pascal
-CBuilder/C++
-C#Builder/C#
-JBuilder/Java
-Kylix
Member Area
-Home
-Account Center
-Top 10 NEW!!
-Submit Article/Tip
-Forums Upgraded!!
-My Articles
-Edit Information
-Login/Logout
-Become a Member
-Why sign up!
-Newsletter
-Chat Online!
-Indexes NEW!!
Employment
-Build your resume
-Find a job
-Post a job
-Resume Search
Contacts
-Contacts
-Feedbacks
-Link to us
-Privacy/Disclaimer
Embarcadero
Visit Embarcadero
Embarcadero Community
JEDI
Links
Create without worrying of destroy a component Turn on/off line numbers in source code. Switch to Orginial background IDE or DSP color Comment or reply to this aritlce/tip for discussion. Bookmark this article to my favorite article(s). Print this article
20-Sep-02
Category
VCL-General
Language
Delphi 4.x
Views
60
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: KK Gian Gian

How can I prevent resources leak.

Answer:

Make use of Interface characteristic where when the reference goes out of scope it 
will free itself.

1   type
2     IAutoClean = interface
3       ['{61D9CBA6-B1CE-4297-9319-66CC86CE6922}']
4     end;
5   
6     TAutoClean = class(TInterfacedObject, IAutoClean)
7     private
8       FObj: TObject;
9     public
10      constructor Create(AObj: TObject);
11      destructor Destroy; override;
12    end;
13  
14  implementation
15  
16  constructor TAutoClean.Create(AObj: TObject);
17  begin
18    FObj := AObj;
19  end;
20  
21  destructor TAutoClean.Destroy;
22  begin
23    FreeAndNil(FObj);
24    inherited;
25  end;
26  
27  Application....
28  
29  procedure TForm1.Button1Click(Sender: TObject);
30  var
31    a: IAutoClean;
32      //must declare as local variable, so when this procedure finished, it's out of 
33  scope
34    o: TOpenDialog; //any component
35  begin
36    o := TOpenDialog.Create(self);
37    a := TAutoClean.Create(o);
38    if o.Execute then
39      ShowMessage(o.FileName);
40  end;


			
Vote: How useful do you find this Article/Tip?
Bad Excellent
1 2 3 4 5 6 7 8 9 10

 

Advertisement
Share this page
Advertisement
Download from Google

Copyright © Mendozi Enterprises LLC