Author: Peter Below
If I want automatic garbage collection with interfaces, I use TInterfacedObject as
base class. What should I use, if I don't want automatic destruction? Is there a
similar common base class or do I have to implement the IInterface methods myself?
Answer:
{BaseNonRefcountIntfObjU:
This unit provides a base class with a non-reference counted
implementation of IUnknown.
Author: Dr. Peter Below
Version 1.0, created 28 März 2002
Last modified: 28 März 2002}
1 unit BaseNonRefcountIntfObjU;
2 3 interface4 5 type6 {Derive classes that need a non-reference counted IUNknown7 implementation from this class.}8 TNonRefcountInterfacedObject = class(TObject, IUnknown)
9 protected10 {IUnknown}11 function QueryInterface(const IID: TGUID; out Obj): HResult; stdcall;
12 function _AddRef: Integer; stdcall;
13 function _Release: Integer; stdcall;
14 end;
15 16 implementation17 18 uses19 Windows;
20 21 function TNonRefcountInterfacedObject.QueryInterface(const IID: TGUID; out Obj):
22 HResult;
23 begin24 if GetInterface(IID, Obj) then25 Result := S_OK
26 else27 Result := E_NOINTERFACE;
28 end;
29 30 function TNonRefcountInterfacedObject._AddRef: Integer;
31 begin32 Result := -1; {-1 indicates no reference counting is taking place}33 end;
34 35 function TNonRefcountInterfacedObject._Release: Integer;
36 begin37 Result := -1; {-1 indicates no reference counting is taking place}38 end;
39 40 end.