Why do interface implementations based on TComponent leak memory?

前端 未结 3 1850
面向向阳花
面向向阳花 2021-02-04 04:09

This Delphi code will show a memory leak for an instance of TMyImplementation:

program LeakTest;

uses
  Classes;

type
  MyInterface = interface
  end;

  TMyIm         


        
3条回答
  •  长情又很酷
    2021-02-04 04:39

    Differences in implementation

    • TComponent._Release does not free your instance.
    • TInterfacedObject._Release does free your instance.

    Perhaps someone can chime in but my take on this is that TComponent is not meant to be used as a reference counted object the way we normally use interfaces.

    Implementation of TComponent._Release

    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;
    

提交回复
热议问题