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.
Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
|
What do you think about this topic? Send feedback!
|