TDictionary 与 TObjectDictionary
2024-10-16 01:01:50
TDictionary 与 TObjectDictionary 的区别是 : TObjectDictionary 可以做到 free的时候 里面的对象 一并free,从而不会出现内存 泄露。
用途:
TDictionary 适合 内存自管理的东西 如:integer int64 word string 结构体 与 动态数组(基本类型与结构体)。如下用法:
TObjectDictionary<string, RPerson>.create();
TObjectDictionary 适合 对象 等内存 手动管理的东西,一般就是 对象。如下用法:
可以发现它有三个构造函数;ACapacity 的意思是 构造的时候 首先创建几个对象。就是说 静态的创建映射类。可以做到映射类创建的时候 事先就内置几个 对象。
Ownerships 的意思是 key或value 跟随 字典一并释放,是个集合参数,那么可以集合为空 或 key 或 key+value 或 value 4种情况。
空集合的时候 表示 key 和 value 都不跟随字典一并释放,需要手工释放。
doOwnsKeys ---- 表示key 跟随字典一并释放。
doOwnsValues --- 表示value 跟随字典一并释放。
demo如下,可以参见:
unit Unit5; interface uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.StdCtrls, System.Generics.Collections; type
TForm5 = class(TForm)
Memo1: TMemo;
Button2: TButton;
procedure FormCreate(Sender: TObject);
procedure Button2Click(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end; TPerson = class
public
name: string;
age: Integer;
end; var
Form5: TForm5; implementation {$R *.dfm} procedure TForm5.Button2Click(Sender: TObject);
var
map: TObjectDictionary<string, TPerson>;
map2: TObjectDictionary<TObject, TPerson>;
keyStr: string;
tempButton: TObject;
begin
//这么写会报错,因为key是string类型的内存释放自管理的, 不能交由字典来管理, 值是object类型的可以
//map := TObjectDictionary<string, TPerson>.Create([doOwnsKeys,doOwnsValues]);
map := TObjectDictionary<string, TPerson>.Create([doOwnsValues]); //注意这里就得写 doOwnsKeys,doOwnsValues] 了,因为key 与 value 都是 对象, 要想map2释放的时候 key与value连同释放就得如此
map2 := TObjectDictionary<TObject, TPerson>.Create([doOwnsKeys, doOwnsValues]);
try
map.Add('', TPerson.Create);
map.Add('', TPerson.Create);
map[''].name := '小李飞刀';
map[''].name := '火云邪神'; for keyStr in map.Keys do
begin
Memo1.Lines.Add(keyStr + '------' + map[keyStr].name );
end; map2.Add(TButton.Create(nil), TPerson.Create);
map2.Add(TButton.Create(nil), TPerson.Create);
for tempButton in map2.Keys do
begin
TButton(tempButton).Top := Random();
TButton(tempButton).Left := Random();
TButton(tempButton).Caption := '测试';
TButton(tempButton).Parent := Self; end;
Application.ProcessMessages;
Sleep() ; // 注意观察 button 会 4秒后 消失。
finally
map.Free;
map2.Free;
end;
end; procedure TForm5.FormCreate(Sender: TObject);
begin
ReportMemoryLeaksOnShutdown := True;
end; end.
最新文章
- linux 如何对文件解压或打包压缩
- ubuntu 常见问题
- linux 运维必备150个命令
- 屏蔽input导致的回车提交事件
- js argument实参集合与局部变量、参数关系
- Android ADB server didn&#39;t ACK * failed to start daemon * 简单有效的解决方案
- Mvc中把list从View传入Controller
- c#接口定义与应用
- Data Annotation
- php中const与define的区别
- python3 第二十一章 - 函数式编程之return函数和闭包
- Elasticsearch学习笔记二
- chrome gps位置模拟设置
- JAVA面向对象设计中类关系
- 洛谷 P2256 一中校运会之百米跑
- [TC14126]BagAndCards
- [ci]jenkins server启动,通过jnlp的方式启动slave(容器模式)
- mysql批量进行optimize table操作
- python的_thread模块来实现多线程(<;python核心编程例子>;)
- 根据IP获取所在的国家城市
热门文章
- [SDOI2009] HH去散步 (矩阵乘法)
- easyUI datetimebox 自定义显示格式
- VS生成事件执行XCOPY时出现Invalid num of parameters的解决方案
- Hadoop生态圈-HBase性能优化
- Java基础-Collection子接口之Set接口
- NDKr10的各种BUG
- (转)tomcat+nginx+redis实现均衡负载、session共享(一)
- MESS-配置
- bzoj千题计划122:bzoj1034: [ZJOI2008]泡泡堂BNB
- HDU 3802 矩阵快速幂 化简递推式子 加一点点二次剩余知识