Consider this short Delphi procedure:
procedure TfrmXQuery.FieldListFillFromDefault;
var
field_list: TStringList;
begin
try
if x <> '' then begin
field_list := TStringList.Create;
{do some stuff with field_list}
end;
finally
if field_list <> NIL then
begin
field_list.Free;
end;
end;
end;
当我在Delphi 3中运行此代码,并将x设置为'',以便field_list从未创建时,
- 为什么
field_list <> NIL
? - 对象没有初始化为
NIL
吗? - 如果它不是
NIL
,那它是什么? - 如果它未被分配且不是
NIL
,我如何知道是否需要Free
它?Assigned
函数无法告诉我:if Assigned(an_object)
等同于if an_object = NIL
if Assigned(Foo) then Foo.Free
本质上等同于if Assigned(Foo) then if Assigned(Foo) then Foo.Destroy
,因为X.Free
基本上是if Assigned(X) then X.Destroy
。 - Andreas RejbrandAssigned(foo)
将返回true,但是foo未初始化,因此foo.Free
将失败。 - Jonathan Elkins