SRL Archive - OSR Help

Freeing form components


Bad Boy JH

2011-02-28 19:54:24

How should i free form components, should I do it like you create…

DeleteButton := TButton.Create(F_MainForm);
DeleteButton := TButton.Free;

or should I do it like this
DeleteButton.Free

Coh3n

2011-02-28 19:57:18

You don't need to do that at all. Since DeleteButton is created with F_MainForm as it's parent, it will get freed when you free F_MainForm. So you just have to call this when the form is closed.

F_MainForm.Free;

Bad Boy JH

2011-03-01 02:14:08

However I am removing individual components from the form…
should I bother?

Dan Cardin

2011-03-01 06:54:58

Probably not since i doubt that it will make a difference, but if you have a lot lot lot of components i suppose you could.

And to my knowledge, the script frees the form at the end of the script anyways. So really all you need to do is put the form loading into a try finally loop and free the form in the finally so that if there is some problem, it will still be freed. Or at least that's what I do/hope it does. So moral of the story. You very likely don't need to do any freeing.

Gu3st...

2011-03-01 08:39:36

To answer your question: DeleteButton.Free; is the proper one

Nava2

2011-03-01 08:41:28

Delete them as you stop needing them. Be sure to create things with parents that you keep track of. Free it at the end.

All that being said, Simba does not track forms.

nielsie95

2011-03-01 09:31:38

You guys talking about parent probably mean owner.


DeleteButton := TButton.Create(F_MainForm);
//Creates a button with F_MainForm as owner
//This means F_MainForm will free it when F_MainForm gets destroyed

DeleteButton.Free();
//Use this if you want to free it before that

Parent is the object which a component is displayed on (to place a button inside a groupbox you need to set the button's parent to the groupbox, but the button may have a whole other component (or nil) as owner).

Coh3n

2011-03-01 17:13:35

From: Bad Boy JH
However I am removing individual components from the form…
should I bother?

I would. If by removing you mean getting rid of them while you're still using the form, instead of doing that, you could just do Button.Visible := False. It also depends on how often you actually want to do that.

Bad Boy JH

2011-03-01 19:02:35

Its just incase the user wants to do anything stupid, I have set it up to do so.