delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
489 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi



Loremo - the 1.5 liter car coming in 2009




Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


Improving your Object classes reliabilityFormat this article printer-friendly!Bookmark function is only available for registered users!
How to reference count the easy way
Product:
Delphi 4.x (or higher)
Category:
OO-related
Skill Level:
Scoring:
Last Update:
03/27/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet Reference-Count Objects Framework reliability
Times Scored:
3
Visits:
2227
Uploader: david bolton
Company:
Reference: N/A
 
Question/Problem/Abstract:
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...)


Var cvList : Tlist;

Const InTidy : boolean =false;
procedure Remove(V : TCompoundVolume);
var Index : integer;
begin
  If InTidy then
    exit;
  For Index := cvlist.count-1 downto 0 do
    if cvlist[Index] = v then
      cvlist.Delete(Index);
end;

procedure Tidylist;
var Index : integer;
begin
  if InTidy then
    exit;
  InTidy := true;
  for Index := cvlist.count-1 downto 0 do
    if assigned(Cvlist[Index]) then
      begin
        TObject(Cvlist[index]).Free;
        cvlist.Delete(Index);
      end;
  InTidy := false;
end;


In the class creator add this line
  cvList.Add(Self);

and in the destructor add this

   Remove(Self);

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


Initialization
  cvlist := tlist.Create;

finalization
  TidyList;
  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.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
E. DSpirito
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)