Mega Code Archive

 
Categories / Delphi / Keywords
 

Destructor - defines the method used to destroy an object

type Class declaration ... Destructor Destroy; Override; ... end; Description The Destructor keyword defines a destructor procedure Destroy for a class. When freeing an object, the Destructor is called. This allows the object to free any storage or other volatile resources it has acquired. The Name for the destructor is normally destroy, but is not restricted to this. It is very wise to keep to this name. The Override directive must be specified since we are overriding the virtual TObject destroy method. Notes At the end of a destructor, you should call Inherited to invoke the parent destructor. Related commands Class Starts the declaration of a type of object class Constructor Defines the method used to create an object from a class Function Defines a subroutine that returns a value Inherited Used to call the parent class constructor or destructor method Object Allows a subroutine data type to refer to an object method Procedure Defines a subroutine that does not return a value TObject The base class type that is ancestor to all other classes Example code : Example of a destructor used to free up acquired memory // Full Unit code. // ----------------------------------------------------------- // You must store this code in a unit called Unit1 with a form // called Form1 that has an OnCreate event called FormCreate. unit Unit1; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs; type // String holder record TString = string[10]; // Define a container class TWords = class private wordCount : Integer; wordsStart : Pointer; function Get(Index: Integer): string; public property GetWord[Index : Integer] : string read Get; published constructor Create(count : integer); Destructor Destroy; override; end; // The form class itself TForm1 = class(TForm) procedure FormCreate(Sender: TObject); end; var Form1: TForm1; implementation {$R *.dfm} // TWords constructor - build the word array constructor TWords.Create(count: integer); var i : Integer; wordsList : ^TString; begin // Get storage for 'count' strings GetMem(wordsStart, count*SizeOf(TString)); // Fill out this list wordsList := wordsStart; wordCount := count; for i := 1 to count do begin wordsList^ := 'Word '+IntToStr(i); Inc(wordsList); end; end; // TWords destructor - release storage destructor TWords.Destroy; begin // Release memory, if obtained if wordsStart <> nil then FreeMem(wordsStart); // Always call the parent destructor after running your own code inherited; end; // GetWord property read function function TWords.Get(Index: Integer): string; var wordsList : ^TString; begin // Read the word at the given index, if in range if (Index >= 1) and (Index <= wordCount) then begin wordsList := wordsStart; Inc(wordsList, Index-1); Result := wordsList^; end; end; // Main line code procedure TForm1.FormCreate(Sender: TObject); var words : TWords; begin // Create a TWords object words := TWords.Create(4); // Now show the 2nd word in this object ShowMessage('2nd word = '+words.GetWord[2]); end; end. 2nd word = Word 2