1
votes

I am learning Delphi recently but when I want to close a dialog or a form, It works with Close and Free and Destroy commands.

What is the difference between these commands and how should I decide which one to use?

1
What does the manual say?whosrdaddy
A bit open ended this one. Between Free and Destroy you typically use Free because it is nil reference safe. Not important here I guess but important in writing destructors. So between Free and Close it comes down to how you want to manage lifetime for your form.David Heffernan
Always call Free instead of Destroy. That is its purpose. As for Close, that depends on the form.Jerry Dodge

1 Answers

0
votes

Tform.Close

In VCL closes the form depending on its State. If your FFormState is FSModal it will do the cancel Action. If your Form is MainForm, the whole apllication will be terminated and so on. For further informations please look into TCustomForm.Close inside of system.pas. In the end the Result will be that you dont see the Form anymore, but that does not mean your Reference to the Form is destroyed.

.free

free also calls destroy, but like David said it is nil reference safe so in most cases you should use free instead of destroy

.destroy