Disposing a pointer inside "FormClose" procedure [SOLVED] lazarus / 21-General ---------------------------------------------------------------------- pascal111 · 2021-05-03 16:03 Is it appropriate to dispose a pointer inside "TForm1.FormClose" procedure to free the pointer from RAM after closing the program? 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 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin   dispose(card_a); end; ---------------------------------------------------------------------- lucamar · 2021-05-03 16:06 I would say "yes", but it really depends on how (and where) you create/use it. ---------------------------------------------------------------------- wp · 2021-05-03 16:35 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 · 2021-05-03 17:23 Quote from: wp on May 03, 2021, 06:35:40 pmOnClose 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i  CloseAction:=caFree;Close; ---------------------------------------------------------------------- lucamar · 2021-05-03 17:43 Quote from: pascal111 on May 03, 2021, 07:23:15 pmFor 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][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i CloseAction:=caFree;Close; No need to call Close (again). Just set CloseAction to caFree and that's all: 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 procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);begin   dispose(card_a);  CloseAction := caFree; 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) ---------------------------------------------------------------------- pascal111 · 2021-05-03 19:45 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.