Recent

Author Topic: Disposing a pointer inside "FormClose" procedure [SOLVED]  (Read 950 times)

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Disposing a pointer inside "FormClose" procedure [SOLVED]
« on: May 03, 2021, 06:03:57 pm »
Is it appropriate to dispose a pointer inside "TForm1.FormClose" procedure to free the pointer from RAM after closing the program?

Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.  
  4.   dispose(card_a);
  5.  
  6. end;        
  7.  
« Last Edit: May 03, 2021, 11:00:55 pm by pascal111 »
La chose par la chose est rappelé.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Disposing a pointer inside "FormClose" procedure
« Reply #1 on: May 03, 2021, 06:06:17 pm »
I would say "yes", but it really depends on how (and where) you create/use it.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

wp

  • Hero Member
  • *****
  • Posts: 11916
Re: Disposing a pointer inside "FormClose" procedure
« Reply #2 on: May 03, 2021, 06:35:40 pm »
OnClose is a difficult beast because of the parameter CloseAction: normally CloseAction is caHide - this means that the form is not destroyed but hidden; this way the form can be reused later (Form.Show) without having to re-create it again. When you destroy a pointer needed by the form here you will run into problems when you re-show the form later. On the other hand you can set CloseAction to caFree - this means that the form really is destroyed, and destroying your pointer here will be correct.

But why don't you use the event OnDestroy. This fires always when the form is destroyed, and this is the correct place the destroy everything which has been created during the lifetime of the form.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Disposing a pointer inside "FormClose" procedure
« Reply #3 on: May 03, 2021, 07:23:15 pm »
OnClose is a difficult beast because of the parameter CloseAction: normally CloseAction is caHide - this means that the form is not destroyed but hidden; this way the form can be reused later (Form.Show) without having to re-create it again. When you destroy a pointer needed by the form here you will run into problems when you re-show the form later. On the other hand you can set CloseAction to caFree - this means that the form really is destroyed, and destroying your pointer here will be correct.

But why don't you use the event OnDestroy. This fires always when the form is destroyed, and this is the correct place the destroy everything which has been created during the lifetime of the form.

I see why @lucamar didn't provide final opinion about the matter. I'll take the suggestion of "OnDestroy" event. For gaining more knowledge, do you
mean if I have seen to use "OnClose" event for freeing a pointer - for example - that I need to modify the value of "CloseAction" like this:

Code: Pascal  [Select][+][-]
  1.  
  2. CloseAction:=caFree;
  3. Close;
  4.  
  5.  
La chose par la chose est rappelé.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Disposing a pointer inside "FormClose" procedure
« Reply #4 on: May 03, 2021, 07:43:15 pm »
For gaining more knowledge, do you mean if I have seen to use "OnClose" event for freeing a pointer - for example - that I need to modify the value of "CloseAction" like this:
Code: Pascal  [Select][+][-]
  1. CloseAction:=caFree;
  2. Close;

No need to call Close (again). Just set CloseAction to caFree and that's all:
Code: Pascal  [Select][+][-]
  1. procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
  2. begin
  3.   dispose(card_a);
  4.   CloseAction := caFree;
  5. end;

But wp is completely right: the OnDestroy handler is the best place to Free/Dispose of things like that.

Sorry I wasn't very clear in my previous post; I was in somewhat of a hurry (which is no excuse, I know) :-[
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

pascal111

  • Sr. Member
  • ****
  • Posts: 423
  • Un trabajo en equipo para programas serias.
Re: Disposing a pointer inside "FormClose" procedure
« Reply #5 on: May 03, 2021, 09:45:43 pm »
Thanks @lucamar, useful information. No problem if you were hurry for some reasons as long as you'll provide useful information helping the learning process.
La chose par la chose est rappelé.

 

TinyPortal © 2005-2018