I.T. Discussion Community!
-Collapse +Expand
To/From Code
-Collapse +Expand Cross Ref Guide
-Collapse +Expand Members-Only
Sign in to see member-only pages.
   ► KBTo/From GuidesDelphi  Print This     

Destructor (Delphi and Delphi Prism Cross Reference Guide)

By Mike Prestwood

Delphi versus Delphi Prism: A side by side comparison between Delphi and Delphi Prism.

OOP Details

More object oriented (OO) stuff.


[Other Languages] 

General Info: Class Destructor

A special class method called when an object instance of a class is destroyed. With some languages they are called when the object instance goes out of scope, with some languages you specifically have to call the destructor in code to destroy the object, and others use a garbage collector to dispose of object instances at specific times.

Desctructors are commonly used to free the object instance but with languages that have a garbage collector object instances are disposed of when appropriate. Either way, destructors or their equivalent are commonly used to free up resources allocated in the class constructor.

Languages Focus

Are object instances freed with a garbage collector? Or, do you have to destroy object instances.

Delphi:   Free or FreeAndNil

Object Pascal uses a standard virtual destructor named Destroy which is called by the standard Free method. All objects are dynamic, so you need to call MyObject.Free method or the FreeAndNil(MyObject) routine for each object you create.

Syntax Example:  
MyObject: TObject;
MyObject := TObject.Create;
  //Use it...
  //Or use...FreeAndNil(MyObject);
Delphi Prism:  "Finalizer" finalize()

Unlike Delphi, Delphi Prism uses the .Net garbage collector to free managed object instances. Prism does not have nor need a true destructor.

In .Net, a finalizer is used to free non-managed objects such as a file or network resource. In Prism, you use the keyword finalizer to indicate the method is a finalizer. Each class can implement one and only one finalizer with a method name of finalize(), which is the method name used in Java and VB.Net.

Because you don't know when the garbage collector will call your finalizer, For non-managed resources, Microsoft recommends you implement the IDisposable interface and call it's Dispose() method at the appropriate time.

Syntax Example:
  Cyborg = class(IDisposable)
    disposed: Boolean;
    method Dispose(disposing: Boolean);
    method Dispose;
    finalizer Finalize;

Follow PrestwoodBoards on: 

©1995-2020 PrestwoodBoards  [Security & Privacy]
Professional IT Services: Coding | Websites | Computer Tech