[fpc-pascal] Constructors & Destructors 101

Richard Ward roward at mac.com
Fri Apr 3 03:43:46 CEST 2009

I am a little bit unclear about certain things on class constructors  
and destructors.

 From what I read in the language reference guide (chapter 6) and  
runtime utilities document (chapter 29):

1)	All classes descend from TObject; even declaring a new class  
without using TObject as a qualifier.

2)	TObject.Create is static (not declared virtual)

3)	TObject.Destroy is declared virtual

4)	Under most circumstances one should call the Free method to release  
an object since it checks for a nil pointer first.   This method then  
calls the destructor (Destroy) which, after executing your own clean  
up code if any, will then release the memory.  However, the object  
memory reference will not be set to nil.   Testing this out confirms  
this and the variable will still contain the memory address of the  
previously allocated memory block even though the memory block pointed  
to by the variable is not usable for anything.   To ensure that the  
address is set to nil, one should call the "global" sysutil function  
FreeAndNil , (chapter 30 RTL doc).   This routine sets the address to  
nil before calling the free method which then calls the destructor.

5)	Although I can't remember reading it anywhere, I've noticed from my  
own demo programs that the addresses of objects are initially set to  

If I have all of the above correct, then I have the following questions:

A)	The documentation says that for the create constructor:

Description: Create creates a new instance of TObject. Currently it  
does nothing. It is also not virtual, so there is in principle no need  
to call it directly.

What is it meant by: "no need to call [create] directly?"  How do you  
invoke the constructor without calling it?  ... and ... Why is create  
not virtual and the destroy destructor is?

B)	The documentation says that for the destroy destructor:

Description: Destroy is the destructor of TObject. It will clean up  
the memory assigned to the instance. Descendent classes should  
override destroy if they want to do additional clean-up. No other  
destructor should be implemented.

What is it meant by: "No other destructor should be  
implemented?"  ...and...  Does it do "something" while the Create  
constructor doesn't?

If you had two different create constructors (for whatever reason),  
might you not also need two different destroy destructors?   What  
problems might you get into if you did?

C)	Just out of curiosity, am wondering why FreeAndNil is global  
procedure instead of a method/destructor of TObject.   I am guessing  
it is for compatibility with Delphi which may or may not have a reason?

I had other curiosity questions but found a nice web page which covers  
the Delphi Object Model.


I have gotten several basic programs to run without worrying about  
these questions, but wanted to make sure I am not going to get into  
trouble later with some subtle problem cropping up.   thanks for any  
information on this.  - ROW

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20090402/8bea962c/attachment.html>

More information about the fpc-pascal mailing list