加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 大数据 > 正文

delphi – 为什么基于TComponent的接口实现泄漏内存?

发布时间:2020-12-15 10:14:57 所属栏目:大数据 来源:网络整理
导读:此Delphi代码将显示TMyImplementation实例的内存泄漏: program LeakTest;uses Classes;type MyInterface = interface end; TMyImplementation = class(TComponent,MyInterface) end; TMyContainer = class(TObject) private FInt: MyInterface; public prop
此Delphi代码将显示TMyImplementation实例的内存泄漏:
program LeakTest;

uses
  Classes;

type
  MyInterface = interface
  end;

  TMyImplementation = class(TComponent,MyInterface)
  end;

  TMyContainer = class(TObject)
  private
    FInt: MyInterface;
  public
    property Impl: MyInterface read FInt write FInt;
  end;

var
  C: TMyContainer;
begin
  ReportMemoryLeaksOnShutdown := True;

  C := TMyContainer.Create;
  C.Impl := TMyImplementation.Create(nil);
  C.Free;
end.

如果TComponent被替换为TInterfacedObject,并且构造函数更改为Create(),则泄漏消失。这里有什么不同的TComponent?

非常感谢答案。总结一下:很容易,但是错误的说:“如果你使用的是接口,那么它们是引用计数的,因此它们被释放出来。”实际上,实现接口的任何类都可以打破这个规则。 (将不会显示编译器提示或警告。)

解决方法

实施差异

> TComponent._Release不释放你的实例。
> TInterfacedObject._Release确实释放你的实例。

也许有人可以铃声,但我认为,TComponent并不意味着我们通常使用接口的方式被用作引用计数对象。

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;

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读