Reputation:
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 :)
Upvotes: 9
Views: 7441
Reputation: 192
The first one
AboutForm := TAboutForm.Create(AboutForm);
tells the TAboutForm, that it's Owner is an object, which has been assigned to the pointer "AboutForm" before (could also be NIL -> no Owner).
The "Owner" parameter of
TForm.Create(Owner: TComponent)
tells the form, who is it's owner. TComponents register themselves at their owner, if some is provided. So, there is some kind of communication between the owner and it's childs.
Example: If the owner gets freed, he can free it's owned components also.
Therefore,
TAboutForm.Create(Self);
means, that "Self" is the Owner of the new TAboutForm, if (Self is TComponent)
.
TAboutForm.Create(NIL);
is also an acceptable solution, but here, you have to free the component yourself (except for, you create TAboutForm within a MDI-Window).
Upvotes: 2
Reputation: 3711
when you create the Form dynamically, that is the form is available
form.
You have to Pass the owner
of the form.
.. So in your case
AboutForm := TAboutForm.Create(Self);
AboutForm := TAboutForm.Create(nil);
AboutForm := TAboutForm.Create(Application);
is the owner of the for AboutForm
(as you already may know it)
But what is the difference though in using any of these:
here is something from about.com-Form Owner
nil - specifies that no object owns the form - and therefore a developer (you) is responsible for freeing the created form (by calling myForm.Free when you no longer need the form)
Self - specifies the object in which the method is called. If, for example, you are creating a new instance of a TMyForm form from inside a Button's OnClick handler (where this button is placed on a MainForm) - self refers to "MainForm". Thus, when the MainForm is freed - it will also free "MyForm".
Application - specifies a global TApplication type variable created when you run your application. "Application" encapsulates your application as well as providing many functions that occur in the background of the program.
and this also Form Owner
a)Formx.Create(Application) -> Form resources will be freed when application is terminated
b)Formx.Create(Self) -> Form resources are freed when the owner object is destroyed (if Self is a descendand of TComponent)
c)Formx.Create(nil) -> you are responsible for freeing the form.
This is from
a is used by delphi when a form is autocreated
b is handy for a main form that has several child windows that need to close when the main form is closed
c is handy for showing a dialog window
For freeing you can do this ACtion := caFree
onclose of the form.
Upvotes: 5
Reputation: 3079
The TForm.Create takes an Owner
as parameter.
In your first example, AboutForm
is the owner. Which obviously is a bad idea, since it's not created yet.
When Self
is the parameter, the instance that makes the call is the owner.
When Application
is the parameter, the Application is the owner.
When nil
is 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:
AboutForm := TAboutForm.Create(nil);
try
AboutForm.ShowModal;
finally
AboutForm.Free;
end;
Upvotes: 10