The TRefCount class implements the IUnknown interface for an Owner object. The TRefCount class makes it easy to quickly make a class to implement the IUnknown interface by using delegation instead of descending from TInterfacedObject, TComponent or similar class. The Owner object just needs to declare a property of this type with the implements keyword and allocate (and free) the memory for the TRefCount class. For example,
Delphi
property RefCount: TRefCount read FRefCount implements IUnknown;
The TRefCount class will take care of reference counting for the Owner object and free the Owner when the reference count equals zero.
Namespace: RefCount
TObject
RefCount.TRefCount
|
Delphi
|
type
TRefCount = class(TObject)
end;
|
|
Name
|
Description
|
|
Create(TObject)
|
The constructor creates the class and sets the Owner property to the AOwner parameter.
|
|
Destroy
|
Represents the destructor of the TRefCount class.
|
Top
|
|
Name
|
Description
|
|
FreeOnRelease
|
The FreeOnRelease property controls whether the TRefCount class frees its Owner when the reference count ( RefCount.TRefCount.RefCount property)becomes zero. Even if the FreeOnRelease property is false, the reference counting will still go on every time the owner is referenced. By default, this property is True.
|
|
Owner
|
The Owner property specifies the Owner of the class. When the reference count ( RefCount.TRefCount.RefCount property) equals zero (and the FreeOnRelease property is True), the TRefCount instance will free its owner.
|
|
RefCount
|
Tracks the number of references to the Owner object. When the reference count goes to zero and the FreeOnRelease property is True, the Owner will be freed.
|
Top
|
|
Name
|
Description
|
|
QueryInterface(TGUID,Void)
|
The QueryInterface method implements the IUnknown QueryInterface method. Do not call the protected QueryInterface method directly. QueryInterface is called through the IUnknown interface to obtain an interface pointer for the interface identified by the IID parameter. If the owner object supports the requested interface, it is returned in the Obj parameter and QueryInterface returns S_OK. If the owner object does not support the interface, QueryInterface returns E_NOINTERFACE.
After successfully obtaining an interface by calling QueryInterface, clients should increase the reference count by calling the IUnknown _AddRef method.
|
|
_AddRef
|
The _AddRef method implements the IUnknown AddRef method. Do not call the protected _AddRef method directly. _AddRef is called through the IUnknown interface to indicate that another object is using the owner's interface pointer. When the other object is through with the interface, it calls _ Release through the IUnknown interface. This allows the class to free its owner when it is no longer used (when the reference count goes to zero).
_AddRef increments the RefCount.TRefCount.RefCount property.
|
|
_Release
|
The _Release method implements the IUnknown Release method. Do not call the protected _Release method directly. _Release is called through the IUnknown interface to indicate that another object is through with the owner's interface pointer. This allows the class to free its owner when it is no longer used (when the reference count goes to zero).
_Release decrements the RefCount.TRefCount.RefCount property.
|
Top
|