마음의 안정을 찾기 위하여 - When do we use Application (Owner), Self or NIL ?
1961392
166
322
관리자새글쓰기
태그위치로그방명록
별일없다의 생각
dawnsea's me2day/2010
색상(RGB)코드 추출기(Color...
Connection Generator/2010
최승호PD, '4대강 거짓말 검...
Green Monkey**/2010
Syng의 생각
syng's me2DAY/2010
천재 작곡가 윤일상이 기획,...
엘븐킹's Digital Factory/2010
When do we use Application (Owner), Self or NIL ?
Delphi | 2007/12/27 10:56
When do we use Application (Owner), Self or NIL ?    
********************************************************************************

Passing the right Owner to a component-constructor
Search Keys:
delphi delphi3000 article borland vcl code-snippet NIL Self owner aowner Times Scored:
 
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;
2007/12/27 10:56 2007/12/27 10:56
Article tag list Go to top
View Comment 0
Trackback URL :: 이 글에는 트랙백을 보낼 수 없습니다
 
 
 
 
: [1] ... [621][622][623][624][625][626][627][628][629] ... [855] :
«   2021/11   »
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30        
전체 (855)
출판 준비 (0)
My-Pro... (41)
사는 ... (492)
블로그... (22)
My Lib... (32)
게임 ... (23)
개발관... (3)
Smart ... (1)
Delphi (75)
C Builder (0)
Object... (0)
VC, MF... (9)
Window... (1)
Open API (3)
Visual... (0)
Java, JSP (2)
ASP.NET (0)
PHP (4)
Database (12)
리눅스 (28)
Windows (23)
Device... (1)
Embedded (1)
게임 ... (0)
Web Se... (2)
Web, S... (19)
잡다한... (5)
프로젝트 (0)
Personal (0)
대통령... (9)
Link (2)