Not Using Reference Counting
Go Up to Memory Management of Interface Objects
If your object is a component or a control that is owned by another component, then it is part of a different memory management system that is based in TComponent. Although some classes mix the object lifetime management approaches of TComponent and interface reference counting, this is very tricky to implement correctly.
To create a component that supports interfaces but bypasses the interface reference counting mechanism, you must implement the _AddRef and _Release methods in code such as the following:
function TMyObject._AddRef: Integer; begin Result := -1; end; function TMyObject._Release: Integer; begin Result := -1; end;
You would still implement QueryInterface as usual to provide dynamic querying on your object.
Note that, because you implement QueryInterface, you can still use the as operator for interfaces, as long as you create an interface identifier (IID). You can also use aggregation. If the outer object is a component, the inner object implements reference counting as usual, by delegating to the "controlling Unknown." It is at the level of the outer object that the decision is made to circumvent the _AddRef and _Release methods, and to handle memory management via another approach. In fact, you can use TInterfacedObject as a base class for an inner object of an aggregation that has a as its containing outer object one that does not follow the interface lifetime model.
Note: The "controlling Unknown" is the IUnknown implemented by the outer object and the one for which the reference count of the entire object is maintained. IUnknown is the same as IInterface, but is used instead in COM-based applications (Windows only). For more information distinguishing the various implementations of the IUnknown or IInterface interface by the inner and outer objects, see Aggregation and the Microsoft online Help topics on the "controlling Unknown."