Recent

Author Topic: Form creation and destroying. Again strange behaviour  (Read 1583 times)

madref

  • Hero Member
  • *****
  • Posts: 1078
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Form creation and destroying. Again strange behaviour
« on: June 26, 2024, 12:48:42 am »
I am trying to open an old form I created in my main program.
In the unit where I am opening the form I am using this code:
Code: Pascal  [Select][+][-]
  1.                          fer2019 := TForm_Evaluaties_2019_Referee.Create(Self);
  2.                          fer2019.Evaluatie_ID := Save_ID;
  3.                          fer2019.Wedstrijd_ID := Save_WD;
  4.                          fer2019.Show;
This opens and shows the form.


When I am done with the form I naturally close it with the following code:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Evaluaties_2019_Referee.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   CloseAction := caFree;
  4. end;     // FormClose


My question is: Is the closeaction destroying the form so that the memory it uses is being freed?
« Last Edit: June 26, 2024, 02:20:29 am by madref »
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Form creation and destroying
« Reply #1 on: June 26, 2024, 01:35:04 am »
Yes, it should be.

All normal close actions should be taking place.

It will not nullify the instance.

« Last Edit: June 26, 2024, 01:36:45 am by jamie »
The only true wisdom is knowing you know nothing

madref

  • Hero Member
  • *****
  • Posts: 1078
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Form creation and destroying. Again strange behaviour
« Reply #2 on: June 26, 2024, 02:26:15 am »
I discovered again a strange behaviour.
When I am trying to close a form through a button on my ribbon I get an error. But when I close the form through the red closing dot (I have a Mac) the form closes as it should.


Is there an explanation for?


The code is use is this:
Code: Pascal  [Select][+][-]
  1. procedure TForm_Evaluaties_2019_Referee.Evaluatie_Referee_2019_Sluiten;
  2. begin
  3.   Form_Evaluaties_2019_Referee.Close;
  4. end;     // Evaluatie_Referee_2019_Sluiten
  5.  
  6.  
  7. procedure TForm_Evaluaties_2019_Referee.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  8. begin
  9.   Form_Lint.vs_Linten.Tag := Form_Lint.vs_Linten.Tag - 1;
  10.   Form_Lint.Btn_Actie_Cancel.Enabled := True;
  11.   Form_Lint.Btn_Actie_Update.Enabled := True;
  12.   Form_Lint.Btn_Actie_OK.Caption := 'OK & Close';
  13.     Form_Lint.Lint_Acties.Hide;
  14.     Form_Lint.Lint_Aanzetten (Form_Lint.vs_Linten.Tag);
  15.   CloseAction := caFree;
  16. end;     // FormClose
  17.  
  18.  
  19. procedure TForm_Evaluaties_2019_Referee.FormShow(Sender: TObject);
  20.  
  21. begin
  22.   Color := Achter;
  23.   PageControl_Evaluatie.TabIndex := 0;
  24.   // Zet de juiste tags bij de juiste knoppen in de ribbon
  25.   Form_Lint.vs_Linten.Tag := Form_Lint.vs_Linten.Tag + 1;
  26.   Form_Lint.Btn_Actie_Cancel.Enabled := False;
  27.   Form_Lint.Btn_Actie_Update.Enabled := False;
  28.   Form_Lint.Btn_Actie_OK.Caption := 'Close';
  29.   Form_Lint.Lint_Acties_Zetten (19);
  30.   Form_Lint.Lint_Acties.Caption := 'Evaluation Actions';
  31.   Form_Lint.Lint_Acties.Tag := Form_Lint.vs_Linten.Tag;
  32.   Form_Lint.Lint_Acties.Show;
  33.   Form_Lint.Lint_Aanzetten (Form_Lint.vs_Linten.Tag);
  34.   if Form_Evaluaties_Overzicht.Showing then begin
  35. //    Form_Evaluaties_Overzicht.Enabled:= False;
  36.     Form_Evaluaties_Overzicht.FormStyle := fsNormal;
  37. //    Form_Evaluaties_Overzicht.Enabled:= True;
  38.     FormStyle := fsStayOnTop;
  39.   end;  // if
  40. end;     // FormShow
  41.  
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

jamie

  • Hero Member
  • *****
  • Posts: 6734
Re: Form creation and destroying. Again strange behaviour
« Reply #3 on: June 26, 2024, 03:11:51 am »
That doesn't tell me much about it.

 Where does the debugger stop? Ine the close event or in your ribbon code?

 Is it possible you have items that are linked to your ribbon code from the form you are closing?

 You may need a delayed close for the form when doing this from the ribbon, so you are not still in the ribbon event.

 I don't know if the Mac widget you are using works the same as windows but what should happen is the CLOSE should be sending a LM_CLOSE message to form you are closing, this gets cached in the message que and comes later, most likely when you have exited the event of your Ribbon.

  The widget should be posting a message that gets there a little later.

  But I would study what your ribbon is doing and how these forms are connected.



The only true wisdom is knowing you know nothing

cdbc

  • Hero Member
  • *****
  • Posts: 1645
    • http://www.cdbc.dk
Re: Form creation and destroying. Again strange behaviour
« Reply #4 on: June 26, 2024, 10:17:00 am »
Hi
I'm with @jamie on this one, you can't free the form in an eventhandler of said form.
You have to use the messaging system, using 'PostMessage()' or 'SendMessage()' or alternatively 'QueueAsyncCall()'.
They all insert a message in the messagequeue, it being LM_CLOSE or your own custom-message...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

Zvoni

  • Hero Member
  • *****
  • Posts: 2737
Re: Form creation and destroying. Again strange behaviour
« Reply #5 on: June 26, 2024, 10:34:06 am »
I discovered again a strange behaviour.
When I am trying to close a form through a button on my ribbon I get an error.
Sorry, my crystal ball seems to be on a different frequency today.
Can't see WHICH error it is.....
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

madref

  • Hero Member
  • *****
  • Posts: 1078
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Form creation and destroying. Again strange behaviour
« Reply #6 on: June 26, 2024, 07:19:26 pm »
Loopse..... i forgot this screenshot :)
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

madref

  • Hero Member
  • *****
  • Posts: 1078
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Form creation and destroying. Again strange behaviour
« Reply #7 on: June 26, 2024, 07:22:24 pm »
Sorry, my crystal ball seems to be on a different frequency today.


Then your crystal balll also has an error.... mine just works fine but it can't give me the answer I want ;D
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

egsuh

  • Hero Member
  • *****
  • Posts: 1489
Re: Form creation and destroying. Again strange behaviour
« Reply #8 on: June 27, 2024, 06:11:05 am »
I'm not sure following codes will work fine, because after "self.close" then there are no place to return to. If you want to use following form then you have to define
         CloseAction := caNone;   in FormClose event, and free the form somewhere else in the calling part.

Code: Pascal  [Select][+][-]
  1. procedure TForm_Evaluaties_2019_Referee.Evaluatie_Referee_2019_Sluiten;
  2. begin
  3.      // Form_Evaluaties_2019_Referee.Close;
  4.      Self.Close;
  5. end;  

I think if you want to make only one form instance during the application's run, you may have it created (and destroyed) automatically.

Or if you can show the form in modal mode, then you'd better use showmodal instead of show.

Code: Pascal  [Select][+][-]
  1. with TForm_Evaluaties_2019_Referee.Create(nil) do begin
  2.     try
  3.         Evaluatie_ID := Save_ID;
  4.         Wedstrijd_ID := Save_WD;
  5.  
  6.         if  ShowModal = mrOK then begin
  7.               // if Form_Lint is calling form, then you can replace Form_Lint with self.
  8.               Form_Lint.vs_Linten.Tag := Form_Lint.vs_Linten.Tag - 1;
  9.               Form_Lint.Btn_Actie_Cancel.Enabled := True;
  10.               Form_Lint.Btn_Actie_Update.Enabled := True;
  11.               Form_Lint.Btn_Actie_OK.Caption := 'OK & Close';
  12.               Form_Lint.Lint_Acties.Hide;
  13.               Form_Lint.Lint_Aanzetten (Form_Lint.vs_Linten.Tag);
  14.  
  15.              // and others do whatever you want to do here
  16.         end;
  17.     finally
  18.           Free;
  19.     end
  20. end;

Sorry if this is not helpful.

madref

  • Hero Member
  • *****
  • Posts: 1078
  • ..... A day not Laughed is a day wasted !!
    • Nursing With Humour
Re: Form creation and destroying. Again strange behaviour
« Reply #9 on: June 29, 2024, 09:13:27 am »
I can not figure out what I am doing wrong so I solved it differently.


Because I want to show pas evaluation that can not be altered I just added a button to close the form instead of using the ribbon.


Now everything works fine
You treat a disease, you win, you lose.
You treat a person and I guarantee you, you win, no matter the outcome.

Main Platform:
--------------
Mac OS X Sonoma 14.7
Lazarus 3.99 (Lazarus 3.99 (rev main_3_99-2668-g6b352d830e) FPC 3.3.1 x86_64-darwin-cocoa)

Windows 10 Pro
Lazarus 3.99 (rev cbfd80ce39)

 

TinyPortal © 2005-2018