Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
498 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (4)


When do we use Application (Owner), Self or NIL ? Go to Max Kleiner's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Passing the right Owner to a component-constructor
Product:
Delphi all versions
Category:
VCL-General
Skill Level:
Scoring:
Last Update:
06/11/2002
Search Keys:
delphi delphi3000 article borland vcl code-snippet NIL Self owner aowner
Times Scored:
9
Visits:
3492
Uploader: Max Kleiner
Company: kleiner kommunikation
Reference: N/A
 
Question/Problem/Abstract:
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;






Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment
Access Object with Nil owner
    Carlos Eduardo (Jul 26 2007 2:50PM)

How I can access an object created with NIL parameter for future destruction? I can have destruct objects then wasnt created and destructed next with Free method ...
Respond

RE: Access Object with Nil owner
Max Kleiner (Jul 30 2007 11:54AM)

I'm not sure got the point, the parameter NIL or self is just responsible for the memory management, not the access of an object.
When you want to access objects without names, then you pass them on a list:
   with collection do begin
      add(TEnglish.create);
      add(TFrench.create);
      add(TSwiss.create) ;

and for free you must call an iteration:

for i:= 0 to collection.count - 1 do begin
   BusinessClass(collection[i]).Free;
// and the list too    
collection.Free  

The Items of a TList are numbered from 0 to Count-1, that means zero based. Above D5 and Kylix, Borland changed the operation of TList with the introduction of a new descendant called TObjectList. They changed only the mechanism of freeing objects in a TList.
If the OwnsObjects property of a TObjectList is set to True (the default), TObjectList controls the memory of its objects (by a new virtual method Notify), freeing an object when its index is reassigned or or when the TObjectList instance is itself destroyed, but the more items in the TList, the longer it takes.  



Respond

Optimisation?
    Enrique Ortuño (Jun 29 2002 12:26AM)

"Passing NIL to create is an optimisation"

How much?

If I use Self parameter and destroy 3 lines above ???
Respond

RE: Optimisation?
max kleiner (Jul 1 2002 1:04PM)

it's more an academic optimisation, just a few microseconds and a DWORD too, cause no owner has to be added by the compiler to the component-list.
Respond














 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
Hans Gulö
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)