初学者的问题: TForm1.Create(nil) 与 TForm1.Create(Application) 的区别是什么? (19分)

  • 主题发起人 拾荒者
  • 开始时间

拾荒者

Unregistered / Unconfirmed
GUEST, unregistred user!
初学者的问题: TForm1.Create(nil) 与 TForm1.Create(Application) 的区别是什么?
 
Hi,

I was wondering if someone could explain the difference between creating a
form or a datamodule with Create(nil) versus Create(Application). Won't
they both be freed when the application terminates? Thanks for any
clarification.

Rob
------------------------------------------------------------------------
If a component's owner is nil, then it will NOT automatically get freed, and
you must free it manually.
------------------------
There is a detailed article on Mark Miller's web site:
www.eagle-software.com.
------------------------
Create(X)--fill in your own X--creates the form and sets the owner to X.
When the owner X is freed, so is the form. So Create(Application) means
that the form will be freed when the application is freed--which is to say,
when the application is shut down.

Create(nil) means that the form has NO owner, so it will NOT be
automatically freed. If you use Create(nil), you are responsible for
freeing the form yourself, e.g.:

Frm := TForm.Create(nil);
try
...
Frm.Show;
...
finally
Frm.Free;
end;

>Won't they both be freed when the application terminates?

If the OS is working properly, then yes, because the OS is *supposed to*
reclaim all the application's resources, including memory, when the
application terminates. It's still good programming practice to free what
you allocate, however--because sometimes the OS *doesn't* do what it's
supposed to.

---------------------------
Sure.

>Shouldn't this pretty well wipe it out?

Are you dead certain that the OS does it?

>Under what circumstances does this not work?

Buggy OS? Whenever it is documented not to work? I have this feeling
somewhere having read the odd exception to the rule you want to depend on.

Read MSDN.
--
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
The "AOwner" Parameter Demystified
Whenever memory is allocated for an object, it must eventually be freed (it’s
a good thing to do). But with components, there is often little mention of the
need to do this. Delphi must be destroying the objects, but how does it know
when we’re through with them? Here's a hint. TComponent declares the Create
constructor as follows:

constructor Create(AOwner: TComponent)
virtual;
The answer is in the "AOwner" parameter to the Create constructor. The Create
constructor is virtual. Every component that resides on the palette inherits
this constructor. Inside the constructor, Delphi creates the component, and if
an owner is specified (if AOwner is not nil), Delphi adds the newly-created
component to the Owner's internal owned component list. This internal list is
consulted by Delphi for three important events that can affect an owner (e.g.,
any TComponent descendant):

A new component is created and added to the internal component list. When this
happens, every component on the internal list is notified by calling the
virtual method, Notification, passing a reference to the new component and
specifying opInsert as the Operation. The Notification method is critical to
support component linking (see the Reuse through Inheritance and Composition
online paper for more information on component linking).

A new component is destroyed and removed from the internal component list. When
this happens, every component on the internal list is notified by calling the
virtual method, Notification, passing a reference to the new component and
specifying opRemove as the Operation.

The owner component is destroyed. When this happens, every owned component on
the internal list is destroyed (and any components owned by these components
are also recursively destroyed). That’s why you don’t need to explicitly
destroy components that are owned, and this is part of what makes programming
in Delphi so easy.


 
值得一看:
http://www.eagle-software.com/DynamicCreation.htm
 
TForm1.Create(nil) 必须自己用Form1.Free 释放对象,以免造成内存泄漏
TForm1.Create(Application) 将对象让Application来管理,当程序结束时
Application会帮你释放该对象

当你想自己管理TForm1对象的建立与释放理请用TForm1.Create(nil),否则
请用TForm1.Create(Application)
 
zw8461回答得很不错
我赞同
 
to all:

鲨鱼的回答正确吗? 因为英文我实在不行。
 
也差不多
 
-----------------------------------------------------
Sender--意义:指本对象。Sender在什么对象相关代码里,那么Sender就是什么。
Self--意义:指本类,也就是Self被引用的类。比如若在类TMyClass内引用了Self,那么Self=TMyClass.
Owner--意义:哪个对象释放我的内存啊?

如:Pan:=TPanel.Create(Self);其中Create的参数是:AOwner:TComponent。Owner释放Pan的内存。因为窗口释放Pan的内存,但窗口类的对象是Self.一般给Owner传Self就可以。
比如:
代码段一:
pan:=TPanel.Create(Self);
with Pan do begin
try
Left:=20;
Top:=20;
parent:=Self
//Parent:=Form1也可以。
Visible:=true;
ShowMessage('Created');
finally
Pan.free;
end;
end;
-----------------------------------------------------
Parent--
意义:此对象包括在哪个对象里那?
说明:若组件不是从TControl继承来的,那么在创建组件后不必声明此属性。

 
TForm1.Create(nil)一般应用于临时窗口的创建,如一个自定义对话框,使用方法如下:
var tmpForm:TForm;
begin
tmpForm := TForm1.Create(nil);
try
... //在此处对tmpForm对象进行操作
finally
tmpForm.Free
//释放tmpForm对象
end;
end;
 
多人接受答案了。
 
顶部