[Lazarus] IUnknown and reference counting

Hans-Peter Diettrich DrDiettrich1 at aol.com
Wed Mar 20 18:56:57 CET 2013


Xiangrong Fang schrieb:
> OK, What confused me is the description found in FPC manual saying:
> 
> TInterfacedObject is a descendent of TObject 
> <http://www.freepascal.org/docs-html/rtl/system/tobject.html> which 
> implements the IUnknown 
> <http://www.freepascal.org/docs-html/rtl/system/iunknown.html> 
> interface. It can be used as a base class for all classes which need 
> reference counting.
> 
> in another word, reference counting is a feature of TInterfacedObject, 
> or the AfterConstruction/BeforeDestruction methods??  it sounds like 
> non-descedants of TInterfacedObject are not reference counted??

TInterfacedObject *simplifies* the addition of interfaces to derived 
classes, by adding the required interface supporting methods to this 
base class. For other classes, which are not based on TInterfacedObject, 
adding interfaces requires that the interface support methods are added 
explicitly to *every* such class.

The compiler inserts calls to the reference counting methods only when 
an interface of a class is *really* used in code. Adding such methods to 
other classes is of no use, because there these methods will never be 
called.

DoDi





More information about the Lazarus mailing list