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. bzoj 1539: [POI2005]Dwu-Double-row

    假设一列交换表示为1,不换表示为0. 身高相同的两个人相当于给其中两列了一个限制条件,要么是两个必须相等,要么一个为零一个为一. 有了关系后我们就可以把每列当成一个点建边,边权为0表示必须相同,1为必 ...

  2. CF17E Palisection——优秀的综合计数题

    题意翻译 给定一个长度为n的小写字母串.问你有多少对相交的回文子 串(包含也算相交) . 输入格式 第一行是字符串长度n(1<=n<=2*10^6),第二行字符串 输出格式 相交的回文子串 ...

  3. redis3.2装完后 其它机子访问爆protocol error, got 'n' as reply type byte

    服务器上装了reids3.2版本,配置文件中已将bind的选项注释掉, linux的iptables的redis端口也开放 其它机子的PHP访问redis爆“protocol error, got ' ...

  4. 第一天:简单工厂模式与UML类图

    何为简单工厂模式:     通过专门定义一个类,来负责创建其他类的实例,这些其它类通常具有共同的父类.   简单工厂模式的UML类图:       简单工厂模式中包含的角色和相应的职责如下:     ...

  5. 在Vista操作系统中通过manifest文件使VC应用程序获得管理员权限

    原文 VC编译出来的应用程序在vista下运行,有可能因为权限问题,不能成功运行. 用以下办法,给应用程序添加一个manifest文件,程序运行时系统就会跳出UAC对话框,获得管理权限. 1.打开应用 ...

  6. linux查看tomcat日志

    声明:以上内容均为转载,个人对这块知识搜罗之后放在一起,非原创,以后这块有问题还会继续添加. Tomcat 日志分为下面5类: catalina . 相当命令行输出日志 localhost . 相当于 ...

  7. vue中的slot与slot-scope

    深入理解vue中的slot与slot-scope vue+element-ui+slot-scope或原生实现可编辑表格 vue插槽详解

  8. Nlog写日志到数据库

    https://github.com/nlog/NLog/wiki/Database-Target

  9. JS的执行和加载(笔记)

    无论当前JS代码是内嵌还是在外链文件中,页面的下载和渲染都必须停下来等待脚本执行完成.JS执行过程耗时越久,浏览器等待响应用户输入的时间就越长.当浏览器遇到内嵌的JS代码时会停止处理页面,先执行JS代 ...

  10. soj1011. Lenny's Lucky Lotto

    1011. Lenny's Lucky Lotto Constraints Time Limit: 1 secs, Memory Limit: 32 MB Description Lenny like ...