Delphi: Problems with TList of Frames

2019-07-29 04:43发布

问题:

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...

Background:

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)

The problem:

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.

Here is my create routine:

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;

And here is my delete routine:

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;

What am I missing?

回答1:

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.

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;


回答2:

If you want to know why your app is taking so long to do something, try profiling it. Try running Sampling Profiler against your program. The helpfile explains how to limit the profiling to only a specific section of your app, which you could use to only get sampling results on the clearing or creating parts. This should show you where you're actually spending most of your time and take a lot of the guesswork out of it.