Is it safe to use Free instead of Release for modal forms in Delphi?

断了今生、忘了曾经 提交于 2019-11-30 08:02:42

问题


The Delphi online help says that Release should be used to remove a form from memory. However, in many examples for modal forms I have seen this construct:

MyForm := TMyForm.Create(nil);
try
  MyForm.ShowModal;
finally
  MyForm.Free;
end;

Is Free a safe way to destroy a modal form? As I can see in the source for ShowModal, Application.HandleMessage will be called until the ModalResult is not 0. Is this the reason why Free can not interfere with pending windows messages?


回答1:


Yes, it's safe to use Free after a ShowModal call.

The cases where you need to use Release are times when you're in the middle of an event handler (eg, OnClick), where further processing after the event will have to access the form. In that case calling Release instead posts a CM_RELEASE message which doesn't free the event until the event handler is done and control has returned to the message pump (ProcessMessages/Application.Run). ShowModal doesn't return until the event handler is finished and control makes it back up the stack, so calling Free afterwards is effectively the same place where the CM_RELEASE message would be processed otherwise.




回答2:


It depends. Freeing the form doesn't call the event handlers that Release does, and any messages that might have been posted to the form and are queued will not be processed. So while, in many and possibly most cases calling Free (or FreeAndNil) will work fine, it may lead to some highly weird behaviour for seemingly random reasons.

The alternative that I'd suggest is in the OnClose event set the Action to caFree, like this:

procedure FormClose(Sender : TObject; Action : TCloseAction)
begin
  Action := caFree;
end;

You can then write code like this:

TMyForm.Create(nil).ShowModal;

And you don't need to free the form specifically, since it'll free itself when it's done.




回答3:


Absolutely, and you can also use the FreeAndNil routine. The FreeAndNil routine will only free the object if it is not already nil, and also set it to nil after the free. If you call free directly on an object which has already been freed, you get an Access Violation.

MyForm := TMyForm.Create(nil); 
try 
  MyForm.ShowModal; 
finally 
  FreeAndNil(MyForm); 
end;


来源:https://stackoverflow.com/questions/916843/is-it-safe-to-use-free-instead-of-release-for-modal-forms-in-delphi

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!