[SOLVED] Questions about TForm.Hide versus TForm.Close lazarus / 31-LCL ---------------------------------------------------------------------- Hartmut · 2025-10-19 08:24 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. ---------------------------------------------------------------------- jamie · 2025-10-19 10:29 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. ---------------------------------------------------------------------- jamie · 2025-10-19 10:31 I also want to comment that a close signal can be sent via message, you can intercept that. ---------------------------------------------------------------------- Hartmut · 2025-10-19 12:39 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 · 2025-10-19 13:08 Quote from: Hartmut on October 19, 2025, 02:39:41 pmHello 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. ---------------------------------------------------------------------- LV · 2025-10-19 13:12 customform.inc: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {---------- --------------------------------------------------------------------     TCustomForm Method Hide------------------------------------------- -----------------------------------}procedure TCustomForm.Hide;begin  Visible := False;end; customform.inc: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i {---------- --------------------------------------------------------------------     TCustomForm Method Close------------------------------------------ ------------------------------------}procedure TCustomForm.Close;var  CloseAction: TCloseAction;  IsMainForm: Boolean;begin  if fsModal in FFormState then    ModalResult := mrCancel  else  begin    if CloseQuery then    begin      // IsMainForm flag set if we are closing MainForm or its parent      IsMainForm := (Application.MainForm = Self) or (Self.IsParentOf(Application.MainForm));      // Prepare default close action      if FormStyle = fsMDIChild then      begin      CloseAction := caNone;        // TODO: mdi logic      end      else      begin        if IsMainForm then          CloseAction := caFree        else          CloseAction := caHide;      end;      // call event handler and let user modify CloseAction      DoClose(CloseAction);      // execute action according to close action      case CloseAction of        caHide: Hide;        caMinimize: WindowState := wsMinimized;        caFree:          begin            // if form is MainForm, then terminate the application          // the owner of the MainForm is the application,            // so the Application will take care of free-ing the form            // and Release is not necessary            if IsMainForm then              Application.Terminate            else              Release;          end;      end;    end;  end;end; ---------------------------------------------------------------------- jamie · 2025-10-19 13:14 Quote from: Hartmut on October 19, 2025, 02:39:41 pmHello 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 ---------------------------------------------------------------------- Hartmut · 2025-10-19 13:46 Quote from: Zoran on October 19, 2025, 03:08:42 pmThere 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? Quote from: jamie on October 19, 2025, 03:14:29 pmYou 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 · 2025-10-19 13:56 Quote from: Hartmut on October 19, 2025, 03:46:32 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 · 2025-10-19 13:58 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 ---------------------------------------------------------------------- Hartmut · 2025-10-19 14:17 Quote from: LV on October 19, 2025, 03:56:42 pmSo 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 · 2025-10-19 15:00 Quote from: Hartmut on October 19, 2025, 03:46:32 pmQuote from: Zoran on October 19, 2025, 03:08:42 pmThere 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. ---------------------------------------------------------------------- Hartmut · 2025-10-19 15:23 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: QuoteAnd 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 · 2025-10-19 19:51 Quote from: Hartmut on October 19, 2025, 05:23:07 pmWhat 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. ---------------------------------------------------------------------- Hartmut · 2025-10-20 07:07 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.