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

推荐订阅源

S
Schneier on Security
有赞技术团队
有赞技术团队
T
The Blog of Author Tim Ferriss
F
Fortinet All Blogs
D
DataBreaches.Net
F
Full Disclosure
腾讯CDC
博客园 - 【当耐特】
MyScale Blog
MyScale Blog
Stack Overflow Blog
Stack Overflow Blog
小众软件
小众软件
Hugging Face - Blog
Hugging Face - Blog
Last Week in AI
Last Week in AI
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
爱范儿
爱范儿
The GitHub Blog
The GitHub Blog
Engineering at Meta
Engineering at Meta
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
S
SegmentFault 最新的问题
The Register - Security
The Register - Security
WordPress大学
WordPress大学
博客园 - 聂微东
雷峰网
雷峰网
J
Java Code Geeks
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
P
Privacy International News Feed
酷 壳 – CoolShell
酷 壳 – CoolShell
A
Arctic Wolf
Scott Helme
Scott Helme
C
Cyber Attacks, Cyber Crime and Cyber Security
T
Tor Project blog
博客园 - 三生石上(FineUI控件)
Know Your Adversary
Know Your Adversary
AWS News Blog
AWS News Blog
G
Google Developers Blog
www.infosecurity-magazine.com
www.infosecurity-magazine.com
C
CERT Recently Published Vulnerability Notes
O
OpenAI News
Project Zero
Project Zero
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
Application and Cybersecurity Blog
Application and Cybersecurity Blog
云风的 BLOG
云风的 BLOG
N
News and Events Feed by Topic
MongoDB | Blog
MongoDB | Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Microsoft Security Blog
Microsoft Security Blog
Cisco Talos Blog
Cisco Talos Blog
P
Palo Alto Networks Blog
Schneier on Security
Schneier on Security

博客园 - 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