Class AReferenceCountedObject

DescriptionHierarchyFieldsMethodsProperties

Unit

Declaration

type AReferenceCountedObject = class(AnObject)

Description

This class represents a reference-counted object; that is, an object that maintains a count of the number of times it has been explicitly referenced. When that count reaches zero, the object is automatically destroyed.

When the object is created, its reference count is initially one 1, since the caller that created the object obtains the first reference to it. The count is incremented every time AReferenceCountedObject.Reference is called, and decremented each time AReferenceCountedObject.free is called. When the count reaches zero, the object is destroyed and ANY REMAINING REFERENCES BECOME INVALID. Ideally, there should be no remaining references at this point, but care must nonetheless be exercised when using reference-counted objects. When the object is passed from one routine to another, the reference obtained by calling AReferenceCountedObject.Reference should be used. Routines that receive a reference should explicitly free it (by calling TObject.free on it) when it is no longer required.

The reference counting scheme used by this class is thread-safe; adjustments to the reference count are protected by a mutex that is local to the instance.

Hierarchy

  • TObject
  • AnObject
  • AReferenceCountedObject

Overview

Fields

Protected myReferenceChange: TRTLCriticalSection;
Protected myReferenceCount: longword;

Methods

Protected procedure adjustReferenceCount(const amount: smallint); virtual;
Protected function setReferenceCount(const newValue: longword): longword; virtual;
Public function init: boolean; override;
Public destructor destroy; override;
Public procedure freeInstance; override;
Public function Reference: AReferenceCountedObject; virtual;
Public function referenceCount: longword; virtual;

Description

Fields

Protected myReferenceChange: TRTLCriticalSection;

Refers to the critical section of code which handles reference counts

Protected myReferenceCount: longword;

Stores the reference count of the object

Methods

Protected procedure adjustReferenceCount(const amount: smallint); virtual;

Adjust the reference counter by the specified amount.

This method increments or decrements Self.referenceCount by the amount specified by amount, in a thread-safe manner.

This method should be called instead of directly operating on the reference count of the instance.

Protected function setReferenceCount(const newValue: longword): longword; virtual;

Set the reference counter to the specified value.

This method sets the value of Self.referenceCount in a thread-safe manner.

This method should be called instead of directly operating on the reference count of the instance.

Returns

The previous value of Self.referenceCount.

Public function init: boolean; override;

Initializer

Public destructor destroy; override;

Destructor.

This method releases the local mutex that is used to protect adjustments to the reference count of the instance.

Public procedure freeInstance; override;

Free the instance.

This method overrides the behavior inherited from TObject.freeInstance: it decrements Self.referenceCount and then checks to see whether the counter has reached zero (0). If so, it calls the inherited routine.

Overriding TObject.freeInstance should prevent the instance from being freed even if its destructor is called directly.

This method decrements the reference count of the instance.

Public function Reference: AReferenceCountedObject; virtual;

Obtain a reference to the object.

This method increments the reference counter associated with the object and then returns a reference to it. The reference returned should be freed by the caller when it is no longer needed.

Public function referenceCount: longword; virtual;

Retrieve the current reference count of the object.


Generated by PasDoc 0.13.0 on 2015-06-23 19:40:11