Title: When do we use Application (Owner), Self or NIL ?
Question: Which owner of a component passed to the constructor is suitable when dealing with run-time dialogs or forms
Answer:
The owner of a component is determined by the parameter passed to the constructor when the component in the VCL or CLX is created. But there are three or four possibilities passing one:
Application, Self, NIL or a Owner by Yourself
1. Passing Application or owner
---------------------------------------------------------------------------
You can set the Owner to application, means when manually creating a form like in our example to assure when the application closes the form is destroyed cleanly.
When you pass Owner to a constructor you want to stay with the component as long as the application stays, for ex.:
dlg:= TmySaveDialogFrm.Create(owner);
This means that when the application is destroyed, all the components are also destroyed.
This means also, passing Owner or application is the same!
dlg:= TmySaveDialogFrm.Create(application);
You can check that in the debugging mode with "pointer(application)" e.g.:
$C11A3C as the reference is the same like "pointer(owner)"
11. Automatic passing
--------------------------
For components created in the form designer by design-time, the form is automatically assigned as the owner to the component.
By default, a form owns all components that are on it. In turn, the form is owned by the application. Thus when the application shuts down and its memory is freed, the memory for all forms (and all their owned components) is also freed.
Hint: Owner is a property of TComponent so you don't have to declare it when passing to the Constructor:
property Owner: TComponent;
constructor TComponent.Create(AOwner: TComponent);
begin
FComponentStyle := [csInheritable];
if AOwner nil then AOwner.InsertComponent(Self);
end;
2. Passing Self
--------------------------------------------------------------------------
Self is useful for a variety of reasons.
Within the implementation of a method, the identifier Self references the object in which the method is called, so the owner is the object.
procedure TfrmIncome.Button3Click(Sender: TObject);
var dlg: TSaveDialog;
XMLFileName: String[150];
begin
Dlg := TSaveDialog.Create(self);
This means that when the form TfrmIncome is destroyed, the component TSaveDialog on the form TfrmIncomeis also destroyed.
When dynamically creating a component as part of a form, the owner has to be the form itself, so you refer to the form via the Self pointer.
3. Passing NIL
--------------------------------------------------------------------------
There is one common special case where you can set the owner to NIL. Imagine a
component is created and destroyed within a single procedure, in UML called an
association between objects, like a loose coupling.
Then you prefer to create the dialog on the fly and no owner is needed; this means that the component has no owner and will never be destroyed by e.g. a Delphi Form . IT's your responsability to free it in a try/finally block after the create method.
Passing NIL to create is an optimisation cause no owner has to be added by
the compiler to the list of components it owns!
dlg:= TSaveDialog.Create(NIL);
dlg.Filter := 'ASCII-Dateien (*.asc)|*.asc';
try
Res := Dlg.Execute;
if Res then
ASCIIFileName := dlg.FileName;
finally
dlg.Free;
end;
4. Passing by Yourself
--------------------------------------------------------------------------
In object terms spoken every control is a component.
When dynamically creating one or more components as part of a dynamic form, the owner has to be the form itself, so you refer to the form via the self pointer and a pointer by yourself (frmMon). eg:
constructor TTransMonitor.create(aOwner: TComponent);
begin
application.onMessage:= showTransMessage;
frmMon:= TForm.create(self);
memMon:= TMemo.create(frmMon);
memMon.parent:=frmMon;
memMon.setbounds(10,10,250,150);
frmMon.caption:=frmTrans.strLit[14];
frmMon.show;
end;