哪个更可取:Free 还是 FreeAndNil? [英] Which is preferable: Free or FreeAndNil?

查看:28
本文介绍了哪个更可取:Free 还是 FreeAndNil?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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?

推荐答案

查看

看看实现:

procedure FreeAndNil(var Obj);
var
  Temp: TObject;
begin
  Temp := TObject(Obj);
  Pointer(Obj) := nil;
  Temp.Free;
end;

示例

考虑以下代码:

procedure TForm1.FormCreate(Sender: TObject);
var
  bm: TBitmap;
begin
  bm := TBitmap.Create;
  bm.LoadFromFile('C:UsersAndreas RejbrandDocumentsRAD Studio6.0DemosDelphiWin32VCLWin32Footballup.bmp');
  bm.Free;

  if Assigned(bm) then
    bm.SaveToFile('C:UsersAndreas RejbrandDesktop	est.bmp')
  else
    ShowMessage('Cannot save! The bitmap does no longer exist!');
end;

这将在我的桌面上创建错误或无效(空)位图,因为我尝试使用已释放的对象.是的,即使 bm 已经被释放,它仍然被分配",即 bm 仍然指向一个内存地址,即使那里没有任何东西(可用).为了克服这一点,可以设置 bm := nil 作为保护措施,然后 assigned(bm) 将返回 false,正如人们所希望的那样.或多或少,FreeAndNil(bm)bm.Free; 的简写;bm := nil.第一条语句释放对象使用的所有内存(以及操作系统资源、CPU 时间等),并且 bm := nil 将指针"bm 设置为 nil,这样 bm 不再指向对象曾经所在的位置,但不再是.这样您(以及诸如 assigned 之类的例程)就不会被愚弄,相信仍然存在位图对象.

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 可能不会花费太多纳秒来执行,实际上 assigned(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.)

这篇关于哪个更可取:Free 还是 FreeAndNil?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆