Articles   Members Online:
-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
How to Improve your Object classes reliability 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
18-Oct-03
Category
Algorithm
Language
Delphi 4.x
Views
153
User Rating
No Votes
# Votes
0
Replies
0
Publisher:
DSP, Administrator
Reference URL:
DKB
			Author: david bolton

One of the worst things you can do is not call a destructor for an object. I found 
this the hard way with my article on Compound Volumes. The destructor call ensured 
that any new additions to the file were properly recorded. So forgetting it caused 
corruption if new files were added.

Answer:

So what we want is a way to call the destructor automatically if you forget to do 
it. Now I could be accused of encouraging lazy programming. So what you should do 
is put a ShowMessage call saying something like “*Oi dipstick, you haven’t called a 
destructor”. That way you avoid corrupting data and your mistakes are found a bit 
easier. 

Heres the main code to be added after the implementation section: 
Note that calling TObject(Pointer).Free works for all objects. (Unless you know 
better...) 

1   var
2     cvList: Tlist;
3   
4   const
5     InTidy: boolean = false;
6   
7   procedure Remove(V: TCompoundVolume);
8   var
9     Index: integer;
10  begin
11    if InTidy then
12      exit;
13    for Index := cvlist.count - 1 downto 0 do
14      if cvlist[Index] = v then
15        cvlist.Delete(Index);
16  end;
17  
18  procedure Tidylist;
19  var
20    Index: integer;
21  begin
22    if InTidy then
23      exit;
24    InTidy := true;
25    for Index := cvlist.count - 1 downto 0 do
26      if assigned(Cvlist[Index]) then
27      begin
28        TObject(Cvlist[index]).Free;
29        cvlist.Delete(Index);
30      end;
31    InTidy := false;
32  end;
33  
34  //In the class creator add this line 
35  
36  cvList.Add(Self);
37  
38  //and in the destructor add this 
39  
40  Remove(Self);


And in your unit, add the lines or modify the Initialization/finalization sections 

41  initialization
42    cvlist := tlist.Create;
43  
44  finalization
45    TidyList;
46    cvlist.free;


If your destructor is called by you, the call to Remove will remove it from the 
list. This needs a recursion check in case you forgot to call it and it tries to 
call Remove while the destructor is called from TidyList. That is what the flag 
InTidy guards against. 

*Dipstick is a mild English term of abuse, about the same as tosspot or tosser, but not as bad as say wanker. 

			
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