Recent

Author Topic: How to unset all controls  (Read 822 times)

nikel

  • Sr. Member
  • ****
  • Posts: 272
How to unset all controls
« on: August 26, 2025, 11:25:52 pm »
Hello, I'm trying to remove controls from memory when form closes. I created a lot of TImages as: Image:=TImage.Create(self); dynamically.

Here's my code so far:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. var
  3.   I         : Integer;
  4. begin
  5.   for I:=0 to ComponentCount - 1 do
  6.   begin
  7.     if (Components[I] is TImage) or (Components[I] is TPanel) then
  8.     begin
  9.        // Components[I]:=nil;
  10.        Components[I].Free;
  11.     end;
  12.   end;
  13. end;

This is giving me error EListError: List index (8) out of bounds. And Components[ I ]:=nil; is giving me another error.

How can I free memory when form closing?

jamie

  • Hero Member
  • *****
  • Posts: 7663
Re: How to unset all controls
« Reply #1 on: August 26, 2025, 11:28:03 pm »
If you set the owner of the image control, then that is all you need to do.

When the form is destroyed, all the owned controls will first get destroyed by calling their Destructor.

Jamie
The only true wisdom is knowing you know nothing

Josh

  • Hero Member
  • *****
  • Posts: 1455
Re: How to unset all controls
« Reply #2 on: August 26, 2025, 11:53:18 pm »
Hi

Like Jamie said, your images must be owned by the form to be seen on your form, so form will auto free them anyway when it closes.

also using a for loop is destined to failure

imagine 8 panels, with 10 images on each (88 components).

your for loop is going 0 to 88 ( this is fixed, it does not re-evaluate the component.count value again on each iteration)
when you free a panelit autofrees the images on the panel.
when you get to the index where the images are say 9 in thissimple scenario
there is nothing to free, as they have already been freed by their parent panel being  freed.

if you must iterate use a while or repeat loop as the check is doneon each iteration
ie not tested might need  >=0 or -1  just showing te principle.
Code: Pascal  [Select][+][-]
  1. i:=Component.Count;
  2. while i>0 do
  3. begin
  4.    component[i].free;
  5.    i:=component.count;
  6. end;
  7.  
  8. or
  9.  
  10. while component.count > 0 do  component[0].Free;
  11.  
  12. or if you want a for loop go backwards through the count ie
  13.  
  14. for i := component.count - 1 downto 0 do  component[i].Free;
  15.  
« Last Edit: August 27, 2025, 12:09:55 am by Josh »
The best way to get accurate information on the forum is to post something wrong and wait for corrections.

Bart

  • Hero Member
  • *****
  • Posts: 5713
    • Bart en Mariska's Webstek
Re: How to unset all controls
« Reply #3 on: August 27, 2025, 12:46:18 am »
Like Jamie said, your images must be owned by the form to be seen on your form

That is not what jamie said.
He said that if the image (component) has an owner, then the woner will free the component when the owner gets destroyed.
The owner typically is the form (as in nikel's example), but it may e.g. also be the Application instance.

The form needs to be the parent of the image in order to show it, not the owner, the owner can be nill and the form doesn't care as long as it is the image's parent.

Bart

 

TinyPortal © 2005-2018