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?
Delphi supplies modal forms with the ModalResult property, which we can read to tell how the user exited the form. The following code returns a result, but the calling routine ignores it: var F:TForm2; begin F := TForm2. Create(nil); F. ShowModal; F.
To add a form to your project, select either File > New > VCL Form or File > New > Multi-Device Form, according to the type of application you are creating.
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;
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With