AOwnsObjects = true 就是  objectlist释放的时候,里面的对象一并释放。

TObjectList对象的创建方法有一个参数:
constructor TObjectList.Create(AOwnsObjects: Boolean);
从字面就可理解其意义:拥有对象集与否。帮助文档:
If the OwnsObjects property is set to true (the default), TObjectList controls the memory of its objects, freeing an object when its index is reassigned; when it is removed from the list with the Delete, Remove, or Clear method; or when the TObjectList instance is itself destroyed.

这段话的意思就是TObjectList 的几个删除方法和释放方法都受参数AOwnsObjects的影响。我就常常用 TObjectList来管理对象,很方便。记得第一次用时,没看文档,用的默认参数值,重载其释放方法,结果一直报错,因为在DLL中实现,找了很久才找出缘由。

TList  TObjectList的区别和使用。

,所在的单元。

TList一直就是在Classes里
TObjectList一直就是在Contnrs里 , TObjectList对象的创建方法有一个参数:
constructor TObjectList.Create(AOwnsObjects: Boolean);
从字面就可理解其意义:拥有对象集与否。帮助文档:
If the OwnsObjects property is set to true (the default), TObjectList controls the memory of its objects, freeing an object when its index is reassigned; when it is removed from the list with the Delete, Remove, or Clear method; or when the TObjectList instance is itself destroyed. 这段话的意思就是TObjectList 的几个删除方法和释放方法都受参数AOwnsObjects的影响。
用的默认参数值,释放时ObjectList里的对象一块释放掉. TList,TObjectList 使用——资源释放 TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象可以方便使用,无需指针强制。 丰富了 Notify 针对当前状态处理,比如如果是删除就将该点的引用一并清理掉; procedure TObjectList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if (Action = lnDeleted) and OwnsObjects then
TObject(Ptr).Free;
inherited Notify(Ptr, Action);
end;
看notify方法会发现:如果TOjectList.OwnsObjects=True时(默认的创建),释放的时候会连里面的对象一块释放掉。
TOjectList.OwnsObjects=False时,释放的时候不会释放里面的对象。 在 Tlist 中,有 Clear(),将呼叫 SetCount,SetCapacity;即删除所有。 procedure TList.Clear();
begin
SetCount();
SetCapacity();
end; 当该对象销毁是,也会自动调用Clear() 清除所有。 destructor TList.Destroy;
begin
Clear();
end; 如果从 Tlist 继承也必须实现 Notify() ,方便资源释放,减少麻烦。
List和OjectList释放的区别如下例子:
procedure TForm1.Button1Click(Sender: TObject);
var
list: TObjectList;
i: Integer;
btn: TButton;
begin
list := TObjectList.Create;
for i := to do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+]);
Parent := Self;
end;
list.Add(btn);
end;
ShowMessage('TObjectList 释放时, 会同时释放其中的对象');
list.Free;
end; procedure TForm1.Button2Click(Sender: TObject);
var
list: TList;
i: Integer;
btn: TButton;
begin
list := TList.Create;
for i := to do
begin
btn := TButton.Create(Self);
with btn do begin
Caption := Format('Btn %d', [i+]);
Parent := Self;
end;
list.Add(btn);
end;
ShowMessage('TList 释放后, 其中的对象并未释放');
list.Free;
end;
如果用TObjectList来存储,当调用sort方法时要注意先将owerobject设置为False
总结:如果有一个对象需要用到列表,最好从 TOjectList 开始继承,对资源释放有完善的处理机制。

TObjectList的更多相关文章

  1. Delphi容器类之---TList、TObjectList、TComponentList、TClassList

    转载自:http://blog.csdn.net/iseekcode/article/details/4922001 从Delphi5开始VCL中增加了新的Contnrs单元,单元中定义了8个新的类, ...

  2. Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析

    转载自:http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题,大家可能以为我会谈TListBox控件,那就错了.我要谈的是Delphi ...

  3. TList,TObjectList 使用——资源释放

    TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象 ...

  4. TStack,TQueue,TObjectList,TObjectStack等等

    TStack,TQueue,TObjectList,TObjectStack等等,都在Contnrs.pas单元里,需要手动添加. 不同于TList类,TObjectList对象将销毁任何从列表中删除 ...

  5. 教程-TObjectList.Clear、TStringList.Clear方法对象有没有被释放

    相关资料: http://www.cnblogs.com/rogge7/p/4631796.html delphiTStringList通过AddObject方法添加对象. object里存的只是指向 ...

  6. delphi 组件容器TObjectList代替List

    delphi 组件容器TObjectList代替List TObjectList objList->delete(0); 这个会释放第0行元素的对象 class TTabFormInfo { p ...

  7. DELPHI7中 TObjectList sort排序问题

    网上收集了一点东西 TOBJECTLIST里,有自带的排序功能 TLIST,TSTRINGLIST也有,MS是一样的 SORT里有一个参数: Compare:TListSortCompare 那我们先 ...

  8. TList TObjectList的区别和使用

    所在的单元 TList(Classes.pas) TObjectList(Contnrs.pas) TObjectList对象的创建方法有一个参数: constructor TObjectList.C ...

  9. Delphi 中的自动释放策略-转

    八.使用结构体而不是结构体指针: 很重要 一.指定 Owner 后, 随 Owner 连带释放: //uses Vcl.StdCtrls, Vcl.ExtCtrls; var panel: TPane ...

随机推荐

  1. kafka 多线程消费

    一. 1.Kafka的消费并行度依赖Topic配置的分区数,如分区数为10,那么最多10台机器来并行消费(每台机器只能开启一个线程),或者一台机器消费(10个线程并行消费).即消费并行度和分区数一致. ...

  2. 解题:APIO 2014 序列分割

    题面 拆开式子我们发现切割顺序不影响答案,所以可以设计出一个$dp[i][j]$表示到$i$为止切了$j$刀的最大收益之类的,然后做个前缀和就可以转移了. $dp[i][j]=min(dp[i][j] ...

  3. 命令行 AppleScript 操控 iTerm2

    AppleScript 是什么? AppleScript 是 macOS 下可用于操控其他软件的脚本语言. 参考链接:https://www.iterm2.com/documentation-scri ...

  4. SpringCloud微服务实战-Zuul-APIGateway(十)

    本文转自:http://blog.csdn.net/qq_22841811/article/details/67637786#准备工作 1 API Gateway 2 Zuul介绍 2.1 zuul的 ...

  5. python基础之collections模块

    Counter Counter是一个简单的计数器,可以统计一段字符串中各个元素出现的次数: import collections counter_1=collections.Counter('kjsd ...

  6. openstack指南

    1.openstack官网 http://www.openstack.org/ 2.openstack源码地址 https://github.com/openstack 3.openstack的pac ...

  7. ElastAlert监控日志告警Web攻击行为

    由于公司需要监控web攻击行为,而因某些原因搭不了waf,才不得不用ElastAlert进行告警,此为前提. 一.ELK安装 Elasticsearch 是一个分布式.可扩展.实时的搜索与数据分析引擎 ...

  8. 【记录】css样式

    记录css的样式设置,方便以后使用. 1.绝对定位,自适应父级大小css: .search-icon-delete { background: url('../../assets/images/sea ...

  9. NOIP2011 提高组 Day1

    自测:8:27——11:51 实际得分:100+60+20=180 期望得分:100+60+40=200 T3读错题,失20 http://cogs.pro/cogs/page/page.php?ai ...

  10. Jekins - Hello world,Jekins + Maven + Git + Tomcat 的简单应用

    Java Web 工程 新建一个简单的 Java Web 工程,并提交至 GitHub,可参考 Eclipse 提交工程至 GitHub 下载 jekins.war 在 http://mirrors. ...