局部定义的对象变量是否要写代码来释放??(50分)

  • 主题发起人 主题发起人 kem
  • 开始时间 开始时间
释放不释放在于这个对象数据是否还有用
例如:我常这样用
procdure TSample.dosomething;
var object:TObject;
begin
object:=TObject;
ObjectList.add(Object);
//objectList 为TSample的成员
//Object的生存期管理我想应该注意:
//如果Object:=TObjectList.Create(True)
// object 将由ObjectList管理
//如果Object:=TObjectList.Create;
//要在手动释放Object
 
有点搞笑,
>>请问,在过程内定义的局部对象变量(如:IniFile : TIniFile;),
>>请问,在过程最后处要释放该变量吗??

定义变量怎么会需要程序去释放呢? 只有自己Create了对象,才去Free呀。
 
to xianjun:
并不是在指明了Component的Owner后,其owner将负责在Free时Free其拥有的Component
但并不是说设置了Owner之后你就不能手动Free它了。
而是在指定Parent后Parent将负责它的Free。Owner和Parent是不一样的。

to:varphone:
with TIniFile.Create('file.ini') do
begin
try
Str := ReadString('SEC','Indent','Defalt');
...
finally
Free;//这里Free。
end;
end;
 
Delphi的帮助里说得很明白:
property Owner: TComponent;
Description
Indicates the component that is responsible for freeing this component.
Use Owner to find the owner of a component. [red]When one component owns another,
the memory for the owned component is freed when its owner's memory is freed.
[/red]
This means that when a form is destroyed, all the components on the form are
also destroyed.

Indicates the parent of the control.
property Parent: TWinControl;
Description
Use the Parent property to get or set the parent of this control. [red]The parent
of a control is the control that contains the control.
[/red] For example, if an
application includes three radio buttons in a group box, the group box is
the parent of the three radio buttons, and the radio buttons are the child
controls of the group box.
 

Similar threads

S
回复
0
查看
1K
SUNSTONE的Delphi笔记
S
S
回复
0
查看
900
SUNSTONE的Delphi笔记
S
后退
顶部