System.TObject.DisposeOf

From Appmethod Libraries
Jump to: navigation, search

Object Pascal

procedure DisposeOf; {$IFNDEF AUTOREFCOUNT} inline; {$ENDIF}

C++

void __fastcall DisposeOf(void);

Properties

Type Visibility Source Unit Parent
procedure
function
public
System.pas
systobj.h
System TObject

Description

DisposeOf forces the execution of the destructor code in an object.

The new Object Pascal mobile compilers introduce a new dispose pattern implemented by calling DisposeOf, which executes the destructor code even if there are variables with pending references to the object. After the DisposeOf method is called, the object is placed in a special state, the Disposed state. This means that the destructor is not called again if DisposeOf is called again, or if the reference count reaches zero (the moment in which the memory is released).

The behavior of DisposeOf differs for the two generations of Object Pascal compilers:

  • On the Object Pascal desktop compilers (DCC32, DCC64, DCCOSX), the effect of calling DisposeOf remains the same, as it calls Free.
  • On the Object Pascal mobile compilers (DCCIOS32, DCCIOSARM), the destructor code is executed at the same time as for the Object Pascal desktop compilers, but the memory is managed by the Automatic Reference Counting mechanism.
type
  TMySimpleClass = class
  private
    //
    stringMember: String;
    constructor Create(Text: String);
    destructor Destroy;
  end;

constructor TMySimpleClass.Create(Text: String);
begin
  stringMember := Text;
end;

destructor TMySimpleClass.Destroy;
begin
  // this will be executed on calling the DisposeOf method.
end;

var
  myObject: TMySimpleClass;
begin
  myObject := TMySimpleClass.Create
    ('This is a code snippet indicating the usage of the DisposeOf method');
  if not myObject.Disposed then
    myObject.DisposeOf;
  //Starting here, the object has entered the Disposed state.
end.

See Also