I create my forms at runtime something like this:
AboutForm := TAboutForm.Create(AboutForm);
try
AboutForm.ShowModal;
finally
AboutForm.Free;
end;
But what is the difference though in using any of these:
AboutForm := TAboutForm.Create(Self);
AboutForm := TAboutForm.Create(nil);
AboutForm := TAboutForm.Create(Application);
They all seem to work the same from what I can see but which is correct, or are they all correct, which is generally the best one to use?
Appreciate your comments thanks 🙂
The TForm.Create takes an
Owneras parameter.In your first example,
AboutFormis the owner. Which obviously is a bad idea, since it’s not created yet.When
Selfis the parameter, the instance that makes the call is the owner.When
Applicationis the parameter, the Application is the owner.When
nilis the parameter, the AboutForm doen not have a owner. That is all fine, but in those cases you must remember to free the form yourself.When you do pass in a owner, you actually don’t need to free i explicitly. The owner will free it when the owner is freed.
This is how your code should look like: