TObjectList

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的更多相关文章
- Delphi容器类之---TList、TObjectList、TComponentList、TClassList
转载自:http://blog.csdn.net/iseekcode/article/details/4922001 从Delphi5开始VCL中增加了新的Contnrs单元,单元中定义了8个新的类, ...
- Delphi容器类之---TList、TStringList、TObjectList,以及一个例程的代码分析
转载自:http://blog.csdn.net/jqandjq/article/details/5429137 看了这里标题,大家可能以为我会谈TListBox控件,那就错了.我要谈的是Delphi ...
- TList,TObjectList 使用——资源释放
TOjectList = Class (Tlist); TOjectList继承Tlist,从名字上看就可以知道它是专门为对象列表制作的,那么他到底丰富了那些功能呢? 首先是 TObject 作为对象 ...
- TStack,TQueue,TObjectList,TObjectStack等等
TStack,TQueue,TObjectList,TObjectStack等等,都在Contnrs.pas单元里,需要手动添加. 不同于TList类,TObjectList对象将销毁任何从列表中删除 ...
- 教程-TObjectList.Clear、TStringList.Clear方法对象有没有被释放
相关资料: http://www.cnblogs.com/rogge7/p/4631796.html delphiTStringList通过AddObject方法添加对象. object里存的只是指向 ...
- delphi 组件容器TObjectList代替List
delphi 组件容器TObjectList代替List TObjectList objList->delete(0); 这个会释放第0行元素的对象 class TTabFormInfo { p ...
- DELPHI7中 TObjectList sort排序问题
网上收集了一点东西 TOBJECTLIST里,有自带的排序功能 TLIST,TSTRINGLIST也有,MS是一样的 SORT里有一个参数: Compare:TListSortCompare 那我们先 ...
- TList TObjectList的区别和使用
所在的单元 TList(Classes.pas) TObjectList(Contnrs.pas) TObjectList对象的创建方法有一个参数: constructor TObjectList.C ...
- Delphi 中的自动释放策略-转
八.使用结构体而不是结构体指针: 很重要 一.指定 Owner 后, 随 Owner 连带释放: //uses Vcl.StdCtrls, Vcl.ExtCtrls; var panel: TPane ...
随机推荐
- Vulkan vs OpenGL ES
Vulkan 简介 Vulkan是一个免费开放的.跨平台的.底层的图形API,在一定程度上比AMD Mantle.微软DirectX 12.苹果Metal更值得开发者关注. Vulkan的最大任务不是 ...
- [Codeforces Gym] 100162B Circle of Stones
题意: 桌子上有 n 个石头围成一个环.每个石头都有一种颜色.每种颜色可以由小写英文字母表示.如果每一对相邻的石头都是不同颜色的,则称这 n 个石头构成的环是美丽的.现在,你可以从这 n 个石头中拿走 ...
- LGP4173残缺的字符串
题解 由于有通配符,所以$kmp$失效了: 将通配符看成0,其余字符看成互不相同的数字,$A,B$串对应得到$a,b$数组; 定义: $f(p) = \sum_{i=0}^{m-1} a_{i}b_{ ...
- vmware中centos7设置静态IP
1.vmware—>Edit—>Virtual Network Editor,选中vmnet8-Nat设置,查看网关IP 2.在centos中设置: vi /etc/sysconfig/n ...
- supervisor "unix:///var/run/supervisor/supervisor.sock no such file" 解决方法
如果是没有开启 supervisord 服务的情况下出现这种报错,可以先 systemctl start supervisor 试试, 如果不是,那就 sudo touch /var/run/supe ...
- centos7 U盘安装卡在 starting dracut initqueue hook解决办法
U盘安装centos7启动过程中出现: [ok] Reached target Basic System 或者 [ok] starting dracut initqueue hook 到下一行就不 ...
- Docker入门与应用系列(二)镜像管理
1.1 什么是镜像 简单说,Docker镜像是一个不包含Linux内核而又精简的Linux操作系统. 1.2 镜像从哪里来 Docker Hub是由Docker公司负责维护的公共注册中心,包含大量的容 ...
- Node.js 被分叉出一个项目 — Ayo.js,肿么了
(注:ayo.js叉从Node.js.目前,大量的文档仍然指向Node.js库.) ayo.js是一个JavaScript运行时建立在Chrome的V8 JavaScript引擎.ayo.js使用事件 ...
- np.isin判断数组元素在另一数组中是否存在
np.isin用法 觉得有用的话,欢迎一起讨论相互学习~Follow Me np.isin(a,b) 用于判定a中的元素在b中是否出现过,如果出现过返回True,否则返回False,最终结果为一个形状 ...
- HTTP 错误 500.19 请求的页面的相关配置数据无效 解决办法
"HTTP 错误 500.19 请求的页面的相关配置数据无效" 解决办法 HTTP 错误 500.19 - Internal Server Error无法访问请求的页面,因为该 ...