• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

delphi AOwner自动释放-测试

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

继 《Delphi 中的自动释放策略》拓展

TComponent分枝也引入了所有权的概念,该概念取自组件库。有两个属性支持所有权:Owner和 Components。每个组件都含有一个Owner属性指定其所有者。

一个组件可能会拥有另外的组件,在这种情况下,所有被包含组件都会出现在该组件的Components属性中。

每个组件的构造器都会包含一个参数指定新组件的所有者。如果该引入的所有者存在,那么这个组件就会自动被添加到所有者的Components属性中。

Components属性除了用来存储所包含组件列表外,还可以提供自动释放其所有的组件。一旦一个组件有了一个所有者,那么当其所有者被释放是,它也会自动释放。

例如:

     因为TForm继承自TComponent,所以当一个窗体被销毁时,其所包含的所有组件都会被自动销毁并且释放其所占用的内存(当然,每个组件必须正确地设计其析构器使其能够被自动的清除)。

constructor TComponent.Create(AOwner: TComponent);
begin
  FComponentStyle := [csInheritable];
  if AOwner <> nil then AOwner.InsertComponent(Self);//将自己插入到所属者的所属列表中,方便所属者destroy时把自己释放
end;

destructor TComponent.Destroy;
begin
  Destroying;
  RemoveFreeNotifications;
  DestroyComponents; //释放
  if FOwner <> nil then FOwner.RemoveComponent(Self);
  FObservers.Free;
  inherited Destro
end;
procedure TComponent.DestroyComponents;
var
  Instance: TComponent;
begin
  FreeAndNil(FSortedComponents);
  while FComponents <> nil do //循环释放自己下面所有的Tcomponent
  begin
    Instance := FComponents.Last;
    if (csFreeNotification in Instance.FComponentState)
      or (FComponentState * [csDesigning, csInline] = [csDesigning, csInline]) then
      RemoveComponent(Instance)
    else
      Remove(Instance);
    Instance.Destroy;
  end;
end;

其实核心代码就  AOwner.InsertComponent(Self) 这句。举例:

TMyClassA=class(TButton)
  public
    procedure KKK(); virtual;
  end;
  TMyClassB=class(TMyClassA)
  public
    procedure KKK();override;
  end;
  TMyClassC=class(TMyClassB)
  public
    procedure KKK(); override;
  end
  
procedure TForm1.FormCreate(Sender: TObject);
begin
    A:=TMyClassA.Create(nil);
    B:=TMyClassB.Create(nil);
    C:=TMyClassC.Create(nil);
    B.InsertComponent(TComponent(A));  //释放B时会连带释放A;以为A已经放到了B里面去管理了
end;

 


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
Matlab 绘制二维图发布时间:2022-07-18
下一篇:
利用MATLAB绘制矩阵色块图和热图 - 那抹阳光1994发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap