惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

N
News and Events Feed by Topic
S
SegmentFault 最新的问题
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
Last Week in AI
Last Week in AI
Jina AI
Jina AI
H
Help Net Security
C
Check Point Blog
aimingoo的专栏
aimingoo的专栏
MyScale Blog
MyScale Blog
H
Hackread – Cybersecurity News, Data Breaches, AI and More
Vercel News
Vercel News
L
LangChain Blog
Recorded Future
Recorded Future
F
Full Disclosure
Google DeepMind News
Google DeepMind News
Microsoft Security Blog
Microsoft Security Blog
I
InfoQ
GbyAI
GbyAI
B
Blog RSS Feed
T
The Blog of Author Tim Ferriss
Engineering at Meta
Engineering at Meta
A
About on SuperTechFans
M
MIT News - Artificial intelligence
爱范儿
爱范儿
V
V2EX
Microsoft Azure Blog
Microsoft Azure Blog
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Y
Y Combinator Blog
B
Blog
WordPress大学
WordPress大学
Blog — PlanetScale
Blog — PlanetScale
W
WeLiveSecurity
MongoDB | Blog
MongoDB | Blog
Cloudbric
Cloudbric
N
News and Events Feed by Topic
The Cloudflare Blog
月光博客
月光博客
博客园 - 三生石上(FineUI控件)
有赞技术团队
有赞技术团队
D
DataBreaches.Net
博客园 - 【当耐特】
T
Troy Hunt's Blog
V
Visual Studio Blog
V2EX - 技术
V2EX - 技术
Apple Machine Learning Research
Apple Machine Learning Research
博客园 - 司徒正美
Recent Commits to openclaw:main
Recent Commits to openclaw:main
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
Google Online Security Blog
Google Online Security Blog
The GitHub Blog
The GitHub Blog

博客园 - ypq

cxGrid让指定的某行自动呈选选中的状态 VMware Esxi5.5中嵌套虚拟机的网络设置方法 SQLServer (2005/2008) 日志清理方法 控制cxGrid 主从表的明细只展开一个 关于Delphi cxGrid主从表中从表只能编辑第一条记录的问题 行字段值拼接成字符串 关于cxGrid选中行操作关联数据集的一种方法 delphi中遍历枚举类型的方法 oracle临时表的两种方式 关于PLSQL的存储过程参数 创建variant二维数组 数据库表行转列,列转行终极方案(转) kav 优化设置(转) - ypq - 博客园 [转载]oracle备份与恢复精华资料 ASP.NET学习线路(转) MS-SQLSERVER--错用了LEN()函数 反向解析(PTR)(转): 域名”A记录,MX记录,CNAME记录,TTL值,URL转发”解释 开博了!
安全释放 TreeView的DATA!
ypq · 2008-10-17 · via 博客园 - ypq

Delphi中使用ListView和TreeView的Item中的Data可能被忽略的内存泄漏问题,使用了Data作为指针的数据,在删除项目时,这些内存将不会释放,可以在OnDeletion事件中加入Dispose(Item.Data); 语句来实现释放。

Delphi中大家在使用 ListView和TreeView时,一般都会使用到Item的Data属性来保存大家自己的内部数据,这个Data的声明如下:

property Data: Pointer;

大家可以看到它就是一个指针,使用上一般类似如下代码(以ListView为)

添加时

  PViewItemState = ^TViewItemState;
  TViewItemState = record
    ViewItemId: integer;
  end;

var
  nLoop: integer;
  li: TListItem;
  pp: PViewItemState;
begin
  for nLoop := 0 to 10 do
  begin
    li := ListView1.Items.Add;
    li.Caption := IntToStr(nLoop);
    new(pp);
    pp^.ViewItemId := nLoop;
    li.Data := pp;
  end;
end;

删除相应的项时多数采用

  ListView1.DeleteSelected;  或

  ListView1.Clear;

就结束了操作。

但是这里存在非常大的危险,就是我们的自己分配的Data内容有没有释放,Delphi文档中也没有关于这个问题的说明,所以我做了一个测试,发现Delphi是不自动释放这些数据的,这些未释放的数据在一个包含较多修改的ListView或者TreeView中将非常危险,如果是长期运行的系统将更危险,Delphi文档中也没有提示这些数据需要释放。

其实这个问题容易被大家忽略,解决办法还是有的,而且也不难,这里就要使用到ListView或者TreeView的OnDeletion事件,Delphi中关于OnDeletion的说明如下:

Occurs when an item in the list view is about to be deleted.

Delphi syntax:

property OnDeletion: TLVDeletedEvent;

C++ syntax:

__property TLVDeletedEvent OnDeletion = {read=FOnDeletion, write=FOnDeletion};

Description

Write an OnDeletion event handler to respond when an item in the list is about to be deleted. The Item parameter is the TListItem object from the Items property that is about to be deleted.

也就是说当需要删除项目时会触发这个事件,在VCL Source 中也可以看到调用过程,其实解决方法就是在这个事件中加入一个语句就可以了,并且是不可以缺少的语句,代码如下:

procedure TForm1.ListView1Deletion(Sender: TObject; Item: TListItem);
begin
  Dispose(Item.Data);
end;

这样就可以安全的释放掉Data的内存了。

转载地址:http://www.mcxb.com/NetProgram/cNet/154769.html