How to implement an interface in delphi mode

I read that if a class implements an interface, now it is counted and should not manage its memory by calling free.

However, if you create your own control and implement the interface, how can you prevent it from managing owneryour memory? For example, when you throw it on a form during development, the struggle for link counting and owner memory management?

Thank you for your time.

+4
source share
1 answer

, TInterfacedObject.
, , -1 *).

TComponent, :

TComponent = class(TPersistent, IInterface, IInterfaceComponentReference)  

TComponent :

function TComponent._AddRef: Integer;
begin
  if FVCLComObject = nil then Result := -1
       // -1 indicates no reference counting is taking place
  else Result := IVCLComObject(FVCLComObject)._AddRef;
end;

function TComponent._Release: Integer;
begin
  if FVCLComObject = nil then Result := -1   
  // -1 indicates no reference counting is taking place
  else Result := IVCLComObject(FVCLComObject)._Release;
end;

*) , VCLComObject , ( -1).
VCLComObject nil. , IDE, COM-.
: TComponent.ComObject

, . , , .

, :

DoesNotRefCount:= Supports(MyObject, IInterfaceComponentReference) 
                  and (TComponent(MyObject).VCLComObject = nil);  

_AddRef , , -1, , ref.
ref 0, _AddRef, _Release, , Delphi _AddRef .

, , :

INoRefCounting = interface
 ['{CAD60ADF-C49A-46FB-BB5A-CC54BD22C7EB}']
end;

Delphi TSingletonImplementation, no-ref .
Delphi TObject ( TWhatever) no-ref :

function TMyObject.QueryInterface(const IID: TGUID; out Obj): HResult; {stdcall;}
begin
  if GetInterface(IID, Obj) then Result := S_OK
  else Result := E_NOINTERFACE;
end;

function TMyObject._AddRef: Integer; {stdcall;}
begin
  Result := -1;
end;

function TMyObject._Release: Integer; {stdcall;}
begin
  Result := -1;
end;  


100% , , _AddRef/_Release, VCLComObject.


, , .
, , _AddRef, _Release Destroy, , .

+6

All Articles