Recent

Author Topic: [SOLVED] Questions about TForm.Hide versus TForm.Close  (Read 965 times)

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
[SOLVED] Questions about TForm.Hide versus TForm.Close
« on: October 19, 2025, 10:24:58 am »
If I have a non modal Form, then I see no difference between using 'Hide' or 'Close': in both cases the Form disappears and can be displayed again via 'Show'. Of course:
 - if there is an OnCloseQuery-Event, let's assume that it does not set 'CanClose:=False' and
 - if there is an OnClose-Event, let's assume that it does not set 'CloseAction:=caFree' and
 - if it is the MainForm, let's assume that it would have an OnClose-Event which sets 'CloseAction:=caHide', because otherwise 'Close' would terminate the App.

I want to understand how 'Close' works in opposite to 'Hide'. As so often the documentation https://lazarus-ccr.sourceforge.io/docs/lcl/forms/tcustomform.close.html is too short...

So my Questions are:
 - are there any differences between 'Close' and 'Hide'? (for non modal Forms, assuming above 3 conditions)
 - does it matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not?

And for non modal Forms, 'Close' before 'Free' seems not to be neccecary... (of course, if there are OnCloseQuery- or OnClose-Events, they would not be called, but many Forms don't have them).

Because I want to understand how 'Close' works (I'm asking not whether this would be a good programming style)...
Let me ask these Questions too:
 - are there any things, that 'Close' does, that it is neccecary before 'Free'? (for non modal Forms)
 - does it matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not?

Thanks in advance.
« Last Edit: October 20, 2025, 09:08:03 am by Hartmut »

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: Questions about TForm.Hide versus TForm.Close
« Reply #1 on: October 19, 2025, 12:29:36 pm »
Close gives you an option of how to close it, hide just simply directly hides it  does nothing more.
With close you can free the form which should be a call to release so other items can complete.
, this makes it essier to cleanup and have a auto late free.
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: Questions about TForm.Hide versus TForm.Close
« Reply #2 on: October 19, 2025, 12:31:35 pm »
I also want to comment that a close signal can be sent via message, you can intercept that.
The only true wisdom is knowing you know nothing

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: Questions about TForm.Hide versus TForm.Close
« Reply #3 on: October 19, 2025, 02:39:41 pm »
Hello jamie, as far as I understand, you only repeat what I had already written before.
Or should this answer any of my questions?

Zoran

  • Hero Member
  • *****
  • Posts: 1977
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Questions about TForm.Hide versus TForm.Close
« Reply #4 on: October 19, 2025, 03:08:42 pm »
Hello jamie, as far as I understand, you only repeat what I had already written before.
Or should this answer any of my questions?

There can hardly be anything more to add to Jamie's comment. It's just that.

Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

LV

  • Sr. Member
  • ****
  • Posts: 378
Re: Questions about TForm.Hide versus TForm.Close
« Reply #5 on: October 19, 2025, 03:12:19 pm »
customform.inc:

Code: Pascal  [Select][+][-]
  1. {------------------------------------------------------------------------------
  2.        TCustomForm Method Hide
  3. ------------------------------------------------------------------------------}
  4. procedure TCustomForm.Hide;
  5. begin
  6.   Visible := False;
  7. end;
  8.  

customform.inc:

Code: Pascal  [Select][+][-]
  1. {------------------------------------------------------------------------------
  2.        TCustomForm Method Close
  3. ------------------------------------------------------------------------------}
  4. procedure TCustomForm.Close;
  5. var
  6.   CloseAction: TCloseAction;
  7.   IsMainForm: Boolean;
  8. begin
  9.   if fsModal in FFormState then
  10.     ModalResult := mrCancel
  11.   else
  12.   begin
  13.     if CloseQuery then
  14.     begin
  15.       // IsMainForm flag set if we are closing MainForm or its parent
  16.       IsMainForm := (Application.MainForm = Self) or (Self.IsParentOf(Application.MainForm));
  17.       // Prepare default close action
  18.       if FormStyle = fsMDIChild then
  19.       begin
  20.         CloseAction := caNone;
  21.         // TODO: mdi logic
  22.       end
  23.       else
  24.       begin
  25.         if IsMainForm then
  26.           CloseAction := caFree
  27.         else
  28.           CloseAction := caHide;
  29.       end;
  30.       // call event handler and let user modify CloseAction
  31.       DoClose(CloseAction);
  32.       // execute action according to close action
  33.       case CloseAction of
  34.         caHide: Hide;
  35.         caMinimize: WindowState := wsMinimized;
  36.         caFree:
  37.           begin
  38.             // if form is MainForm, then terminate the application
  39.             // the owner of the MainForm is the application,
  40.             // so the Application will take care of free-ing the form
  41.             // and Release is not necessary
  42.             if IsMainForm then
  43.               Application.Terminate
  44.             else
  45.               Release;
  46.           end;
  47.       end;
  48.     end;
  49.   end;
  50. end;
  51.  
« Last Edit: October 19, 2025, 03:19:49 pm by LV »

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: Questions about TForm.Hide versus TForm.Close
« Reply #6 on: October 19, 2025, 03:14:29 pm »
Hello jamie, as far as I understand, you only repeat what I had already written before.
Or should this answer any of my questions?

You seem to overlook the fact that I made about a Message being sent "WM_CLOSE" that can be done from other apps, that remote message can close your form without your consent unless you use that even control.

Jamie
The only true wisdom is knowing you know nothing

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: Questions about TForm.Hide versus TForm.Close
« Reply #7 on: October 19, 2025, 03:46:32 pm »
There can hardly be anything more to add to Jamie's comment. It's just that.
Do you want to say, that the precise answers to my 4 questions are (conditions see above):
 - there are no differences between 'Close' and 'Hide'
 - it does not matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not
 - 'Close' does nothing, that is neccecary before 'Free'
 - it does not matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not
???
I did not read that from jamies post...



@LV: I already had done a look into the sources, but I did not understand enough. Too many used unknown things, which if I follow them, again use too many unknown things... That's why I started this Topic...
Can you answer some of my questions definitely?



You seem to overlook the fact that I made about a Message being sent "WM_CLOSE" that can be done from other apps, that remote message can close your form without your consent unless you use that even control.
I saw it and I understood, that other apps can close my app via a "WM_CLOSE" message. But this was not a question...

LV

  • Sr. Member
  • ****
  • Posts: 378
Re: Questions about TForm.Hide versus TForm.Close
« Reply #8 on: October 19, 2025, 03:56:42 pm »
@LV: I already had done a look into the sources, but I did not understand enough. Too many used unknown things, which if I follow them, again use too many unknown things... That's why I started this Topic...
Can you answer some of my questions definitely?

Hide -> instantly hides the form, without triggering any events.
Close -> runs OnCloseQuery / OnClose / CloseAction logic.
If CloseAction = caHide, the result is the same as Hide.

So if your form has no OnClose or OnCloseQuery logic, Close and Hide are completely equivalent.

jamie

  • Hero Member
  • *****
  • Posts: 7402
Re: Questions about TForm.Hide versus TForm.Close
« Reply #9 on: October 19, 2025, 03:58:07 pm »
Other apps can also move your form off screen, so you may also want to beware of that in case you have some sensitive app.

 I've run into this already. You need to catch that message too.

Jamie
The only true wisdom is knowing you know nothing

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: Questions about TForm.Hide versus TForm.Close
« Reply #10 on: October 19, 2025, 04:17:00 pm »
So if your form has no OnClose or OnCloseQuery logic, Close and Hide are completely equivalent.
Thanks a lot LV for these precise answer.

So only these 2 questions are left open:
 - are there any things, that 'Close' does, that is neccecary before 'Free'? (for non modal Forms with no OnCloseQuery- or OnClose-Event)
 - does it matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not?

Zoran

  • Hero Member
  • *****
  • Posts: 1977
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Questions about TForm.Hide versus TForm.Close
« Reply #11 on: October 19, 2025, 05:00:53 pm »
There can hardly be anything more to add to Jamie's comment. It's just that.
Do you want to say, that the precise answers to my 4 questions are (conditions see above):
 - there are no differences between 'Close' and 'Hide'
 - it does not matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not
 - 'Close' does nothing, that is neccecary before 'Free'
 - it does not matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not
???
I did not read that from jamies post...


1. the difference is - you call close and leave to the window to decide how to handle it - you do not want to decide whether the form is destroyed or just hidden, you want that decision to be made in other place, by the form itself, not here where you make this call (However, as you clearly put that you assume that the three conditions you gave are satisfied, I don't think you needed this answer).

The bottom line - if the conditions you set are satisfied, there is no difference between the two.

2. It does not matter whether it is the main form or not if your conditions are assumed, otherwise it does matter.
Apart from being the main application form or not, the owner does not matter for close. Whether the owner is the Application instance, any other component, or nil, it does not matter.

3. Under the condition you assume, it has nothing to do with free, it only hides the form.

4. See answer 2.

Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: Questions about TForm.Hide versus TForm.Close
« Reply #12 on: October 19, 2025, 05:23:07 pm »
Thank you Zoran for your help. No. 1 and 2 are now clear. With No. 3 maybe we misunderstood. Let my repeat from what I wrote in my 1st post:
Quote
And for non modal Forms, 'Close' before 'Free' seems not to be neccecary... (of course, if there are OnCloseQuery- or OnClose-Events, they would not be called, but many Forms don't have them).
...
Let me ask these Questions too:
 - are there any things, that 'Close' does, that it is neccecary before 'Free'? (for non modal Forms)
 - does it matter, whether it is a "normal" Form or the MainForm or whether the Owner is 'Application' or not?

What I mean: do I need to call 'Close' before 'Free'?
It seems to make no difference, if I skip 'Close' before 'Free'.
Does 'Close' anything, that is neccecary before 'Free'? (all this for non modal Forms with no OnCloseQuery- or OnClose-Event)

Zoran

  • Hero Member
  • *****
  • Posts: 1977
    • http://wiki.lazarus.freepascal.org/User:Zoran
Re: Questions about TForm.Hide versus TForm.Close
« Reply #13 on: October 19, 2025, 09:51:36 pm »
What I mean: do I need to call 'Close' before 'Free'?
It seems to make no difference, if I skip 'Close' before 'Free'.
Does 'Close' anything, that is neccecary before 'Free'? (all this for non modal Forms with no OnCloseQuery- or OnClose-Event)

First, I'll try to answer your question directly.
If the conditions you set are satisfied, there is no code in OnClose or OnCloseQuery (apart from setting CloseAction to caHide), so the only thing Close will do to a non-modal form is hide it, which doesn't make an actual difference if you are freeing the form immediately after it.
In short - it doesn't make a difference if you skip Close before Free.


Now, although I believe it was the full answer to your question, there is something else I think that should be said here.

There is a situation when you should not call Form.Free directly.
That situation is when the call to Free (or destroy) is made inside some event handler of the form (or event of some component whose owner is the form) -- then the form object might be accessed internally after this event, so the form needs to live a bit longer, until the whole event processing has finished.
Hence, immediate destroying from inside the handler might lead to access violation. In such cases you should call Release method, instead of directly calling Destroy or Free. Calling Release makes sure that all event handlers are processed and only then the form is actually destroyed.

That is why Close method (when CloseAction is caFree) does not call Destroy or Free, but it calls Release.
So, calling Close (which means Hide) before Free is never necessary or useful, but Close with caFree (which means Release) instead of Free might be a better option for destroying the form than calling Free. Note that in this situation, if you call Release (or Close) and then Free immediately after it, you can still get AV -- Release scheduled destroying, but you destroyed it too early nevertheless! -- so, from inside the event handler of the Form, don't call Release (or Close) before Free, but call it instead of Free.

Again, calling Form.Free directly is safe unless it is called from inside some event handler of the Form or an event handler of a component owned by this form.
Swan, ZX Spectrum emulator https://github.com/zoran-vucenovic/swan

Hartmut

  • Hero Member
  • *****
  • Posts: 1028
Re: Questions about TForm.Hide versus TForm.Close
« Reply #14 on: October 20, 2025, 09:07:22 am »
Thank you very much Zoran for this detailed and precise answer. And for mentioning not to call Free() from inside of an Event handler of this Form. I had made this bug in the past, so there I knew it, but it was good being remembered now again of that to not forget this.

 

TinyPortal © 2005-2018