Delphi Basics
Dispose
Procedure
Dispose of storage used by a pointer type variable System unit
1  procedure Dispose ( var VariablePointer : Pointer-Type ) ;
2  procedure Dispose ( var ObjectPointer : Object-Pointer; Destructor ) ;
Description
The Dispose procedure comes in two flavours.
 
The older version is an obsolete method of destroying objects (you should now call the class destructor instead).
 
The first version frees storage used by a pointer type variable VariablePointer.
 
You should use Dispose when no longer using a variable allocated using New.
Notes
Warning : the variable is undefined after calling Dispose. It is not set to nil.
Related commands
FreeMem Free memory storage used by a variable
GetMem Get a specified number of storage bytes
New Create a new pointer type variable
ReallocMem Reallocate an existing block of storage
 
Example code : Allocate memory for a record, assign to it, and then dispose of it
// 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
  // The System unit does not need to be defined
  Forms, Dialogs;
 
type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;
 
var
  
Form1: TForm1;
 
implementation
{$R *.dfm} // Include form definitions
 
procedure TForm1.FormCreate(Sender: TObject);

type
  TCustomer = Record
    name : string[20];
    age  : Byte;
  end;

var
  custRecPtr : ^TCustomer;

begin
  // Create a customer record using 'New'
  New(custRecptr);

  // Assign values to it
  custRecPtr.name := 'Her indoors';
  custRecPtr.age  := 55;

  // Now display these values
  ShowMessageFmt('%s is %d',[custRecPtr.name, custRecPtr.age]);

  // Now dispose of this allocated record
  Dispose(custRecPtr);
end;
 
end.
Hide full unit code
   Her indoors is 55
 
 
© CodeGear 2006 - 2007. All rights reserved.  |  Home Page