问题描述
Free和FreeAndNil之间的基本区别是什么?
What is the basic difference between Free and FreeAndNil?
FreeAndNil = Free + Nil?
Is FreeAndNil = Free + Nil?
我什么时候应该使用Free,何时应该使用FreeAndNil?
When should i use Free and when should i use FreeAndNil?
当有人帮助我时,我没有得到这些。
I am not getting these when goggling can someone help me.
提前感谢
推荐答案
请参阅
- ...
- delphibasics-FreeAndNil
- docwiki.embarcadero-FreeAndNil
- pages-freeandnil
- eurekalog-freeandnil
- blogs.embarcadero
- ...
看看实现:
procedure FreeAndNil(var Obj);
var
Temp: TObject;
begin
Temp := TObject(Obj);
Pointer(Obj) := nil;
Temp.Free;
end;
示例
考虑以下代码:
Examples
Consider the following code:
procedure TForm1.FormCreate(Sender: TObject);
var
bm: TBitmap;
begin
bm := TBitmap.Create;
bm.LoadFromFile('C:\Users\Andreas Rejbrand\Documents\RAD Studio\6.0\Demos\DelphiWin32\VCLWin32\Football\up.bmp');
bm.Free;
if Assigned(bm) then
bm.SaveToFile('C:\Users\Andreas Rejbrand\Desktop\test.bmp')
else
ShowMessage('Cannot save! The bitmap does no longer exist!');
end;
这将在桌面上创建错误或无效(空)位图,因为我尝试使用已被释放的对象。是的,即使 bm
已被释放,仍然是分配,即 bm
仍然指向内存地址即使没有(可用)那里。为了克服这个问题,可以设置 bm:= nil
作为保护措施,然后分配(bm)将返回false ,就像人们想要的那样。或多或少, FreeAndNil(bm)
是 bm.Free的缩写; bm:= nil
。第一个语句释放对象使用的所有内存(和操作系统资源,CPU时间等),而 bm:= nil
设置指针 bm
到 nil
,以便 bm
不再指向对象使用的位置是,但不再是。这样你(和例如分配的例程
)不会被愚弄,相信还有一个位图对象。
This will create an error or an invalid (empty) bitmap on my desktop, because I try to use an object that has been freed. Yes, even though bm
has been freed, it is still "assigned", i.e. bm
still points to a memory adress, even though there is nothing (usable) there. To overcome this, one can set bm := nil
, as a safeguard, Then assigned(bm)
will return false, as one would want. More or less, FreeAndNil(bm)
is a shorthand for bm.Free; bm := nil
. The first statement frees all memory (and OS resources, CPU time etc. used by the object), and bm := nil
sets the "pointer" bm
to nil
, so that bm
no longer points to the place where the object used to be, but no longer is. This way you (and routines like assigned
) will not get fooled to believe that there still is a bitmap object.
有人说你应该总是使用 FreeAndNil(foo)
而不是 foo .Free
。那么为什么不呢额外的说明 foo:= nil
可能不会花费太多的纳秒来执行,实际上 assign(foo)= false
是一个免费的对象的非常好的属性。但是再一次,如果你知道你在做什么,并且知道你在释放之后再也不会使用 foo
对象,那么你可以坚持只是 foo.free
。真的有人会认为,在许多情况下(但不是全部),尝试使用一个释放对象的变量本身就是一个错误。 (当然有些情况下你有意这样做 - 你有一个对象 foo
,有时被分配,有时不是。)
Some say that you should always use FreeAndNil(foo)
rather than foo.Free
. Well, why not? The additional instruction foo := nil
will probably not take too many nanoseconds to execute, and indeed assigned(foo) = false
is a very nice property of a freed object. But then again, if you know what you are doing, and know that you will never use the foo
object again after freeing it, then you could stick to just foo.free
. Really, some would argue that in many cases (but not all), trying to use a variable of a freed object is a bug by itself. (Of course there are cases where you do this intentionally - you have an object foo
that sometimes is assigned and sometimes is not.)
这篇关于哪个更好:免费或FreeAndNil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!