O'Reilly logo

Delphi for .NET Developer’s Guide by Xavier Pacheco

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

The Dispose Pattern—IDisposable

The dispose pattern gives developers an explicit mechanism to release unmanaged resources immediately rather than relying on a GC cycle. This is accomplished by implementing the IDisposable interface.

IDisposable Example

Consider the TCriticalSection class from the previous section. The following is its reworked definition that implements IDisposable:

TCriticalSection = class(TObject, IDisposable)
private
  FSection: TRTLCriticalSection;
  FCSValid: Boolean;
strict protected
    procedure Finalize; override;
public
  constructor Create;
  procedure Dispose;
end;

You should notice that the object now implements the IDisposable interface and its Dispose() procedure. Also, an FCSValid Boolean field will be initialized to False ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required