Delphi:框架的TList问题 [英] Delphi: Problems with TList of Frames

查看:104
本文介绍了Delphi:框架的TList问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在一个由TScrollBox内的许多帧(通常为25个)组成的接口上遇到问题.

I'm having a problem with an interface that consists of a number of frames (normally 25) within a TScrollBox.

有两个问题,我希望一个是另一个的结果...

There are 2 problems, and I am hoping that one is a consequence of the other...

背景:

当应用程序启动时,我创建25个帧,每个帧包含大约25个帧. 20个控件,然后使用默认信息进行填充.然后,用户可以单击控件以将搜索限制为信息的一个子集,此时我可以释放信息并重新创建框架(因为搜索可能返回<25条记录)

When the application starts up, I create 25 frames, each containing approx. 20 controls, which are then populated with the default information. The user can then click on a control to limit the search to a subset of information at which point I free and recreate my frames (as the search may return < 25 records)

问题:

如果在初始搜索后退出了应用程序,则大约需要花费时间. 5秒钟回到德尔福.在第二次搜索(并处理/重新创建帧)之后,大约需要花费大约2秒钟的时间. 20秒)

If I quit the application after the initial search then it takes approx. 5 seconds to return to Delphi. After the 2nd search (and dispose / recreate of frames) it takes approx. 20 seconds)

虽然我可以重写应用程序以仅创建一次框架,但我想了解发生了什么.

Whilst I could rewrite the application to only create the frames once, I would like to understand what is going on.

这是我的创建例程:

procedure TMF.CreateFrame(i: Integer; var FrameBottom: Integer);
var
   NewFrame: TSF;
begin
   NewFrame := TSF.Create(Self);
   NewFrame.Name := 'SF' + IntToStr(i);
   if i = 0 then
      NewSF.Top := 8
   else
      NewSF.Top := FrameBottom + 8;
   FrameBottom := NewFrame.Top + NewFrame.Height;
   NewFrame.Parent := ScrollBox1;
   FrameList.Add(NewFrame);
end;

这是我的删除例程:

procedure TMF.ClearFrames;
var
   i: Integer;
   SF: TSF;
begin
   for i := 0 to MF.FrameList.Count -1  do
   begin
      SF := FrameList[i];
      SF.Free;
   end;
   FrameList.Clear;
end;

我想念什么?

推荐答案

当您通过释放帧来控制要创建的帧的内存分配时,因此无需在其中提供Self作为拥有者参数创建构造函数.相反,请传递nil,以防止所有者尝试释放框架.

As you are taking control over the memory allocation of the Frames you are creating by Free'ing them, so there's no need to provide Self as the owner parameter in the create constructor. Pass nil instead to prevent the owner trying to free the frame.

此外,也不喜欢ClearFrames例程的外观.尝试以下方法:

Also, don't like the look of your ClearFrames routine. Try this instead:

while FrameList.count > 0 do
begin
    TSF(Framelist[0]).free;
    Framelist.delete(0);
end;
Framelist.clear;

这篇关于Delphi:框架的TList问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆