Sign in Join
Wiki copytemporary

Destructor

Deutsch · English · Suomi

The reserved word destructor belongs to object-oriented programming. The destroyer is used to release resources like memory. The destroyer must always be made so that when the memory is released, the memory used by the entire class (object) is released and therefore no memory leak occurs.

Release of the object takes place by calling a class free method. Calling free causes a Destroy invitation. It also checks that the self variable is not nil.

for example:

pascal
program Project1;
{$ mode objfpc} {$ H +}

type

   // class definition
   {TClass}

   TClass = Class
     constructor Create;
     destructor Destroy; override; // allows the use of a parent class destroyer
   end;

// class builder
constructor TClass.Create;
begin
   Writeln ('Build object');
end;

// class eraser
destructor TClass.Destroy;
begin
   Writeln ('Demolished Object');
   inherited; // Also called parent class destroyer
end;

var

// Defines the class variable
   myclass: TClass;

begin
   myclass: = TClass.Create; // Initialize the object by calling the class builder
   Writeln ('Something Code ...');
   myclass.Free; // Free invites your own class Destroy discharger
   Writeln ('Press <Enter>');
   readln;
end.

From the Free Pascal wiki, page "Destructor", as it stood on 2022-08-06 after a change by Kai Burghardt, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's Licensing page. If you have concerns about this material, message the admin.

Revision 1, last changed by WikiArchive .

Talk about this page

Nobody has opened a bench for this page yet. Sign in to open one.