Is it safe to use Free instead of Release for moda

2019-01-25 07:53发布

问题:

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;