我应该在每个Object.Create之后放置一个try-finally块吗? [英] Should I put a try-finally block after every Object.Create?

查看:81
本文介绍了我应该在每个Object.Create之后放置一个try-finally块吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关于OO Delphi中的最佳实践,我有一个一般性的问题.目前,我在创建对象的任何地方都放置了try-finally块,以在使用后释放该对象(以避免内存泄漏).例如:

I have a general question about best practice in OO Delphi. Currently, I put try-finally blocks anywhere I create an object to free that object after usage (to avoid memory leaks). E.g.:

aObject := TObject.Create;
try
  aOBject.AProcedure();
  ...
finally
  aObject.Free;
end;

代替:

aObject := TObject.Create;
aObject.AProcedure();
..
aObject.Free;

您认为这是一个好习惯,还是开销太大?那性能呢?

Do you think it is good practice, or too much overhead? And what about the performance?

推荐答案

我认为,对象构造 不应 的唯一原因是(<或输入",如梅森指出的).

In my opinion, there is only one reason an objects construction should not be followed by (or "in" as Mason pointed out) a try / finally block.

  1. 如果某个对象的生存期由另一个对象管理.

此管理可以采用三种形式:

This management can take three forms:

  1. 对象的引用的作用域超出本地块,并且在其他地方释放,就像在析构函数中释放的字段成员一样.
  2. 立即添加到列表中的对象,负责稍后释放对象.
  3. 具有关联的生存期管理器的对象,例如如何将所有者传递给构造中的VCL控件.

#1 中,当引用的范围更广时,如果没有立即构建引用,则应立即将其设置为nil.这样,当您将其作为参考时,您便知道自己的读数是准确的.这对于成员对象而言是最常见的,这些成员对象是作为较大类的一部分构造的,然后在父对象被销毁时进行清理.

With #1, when the reference has a broader scope, the reference should be set to nil immediately if it isn't constructed right away. That way when it is checked for reference you know you have an accurate reading. This is most common with member objects that are constructed as part of a larger class, and then cleaned up when the parent object is destroyed.

对于#2 将对象添加到列表中时,您只想使用try-except块(我使用过几次)之一如果在构造对象之后且将其添加到管理列表之前发生异常.理想情况下,构造后的第一行将其添加到列表中,或者列表实际上是一个工厂类,为您提供一个已经添加到列表中的对象.

With #2, when an object is added to a list, you want to use a try-except block (one of the few times I use one) just in case an exception occurs after the object is constructed and before it is added to the managing list. Ideally the first line after the construction is adding it to the list, or the list is actually a factory class that gives you an object already added to the list.

使用#3 当对象具有另一个生命周期管理器时,您确实应该确保由该管理器进行管理是正确的做法.如果要构造VCL控件,则可能会想让窗体(或任何其他控件)拥有它,但这实际上在构造和销毁方面增加了额外的开销.如果可能的话,您应该显式地释放它,尤其是如果您一次放置该控件,那么您知道将在窗体的析构函数中或在它关闭时释放它.唯一无法执行的操作是控件的创建更加动态.

With #3, when an object has another lifetime manager, you really should make sure having it managed by that manager is the right thing to do. If you are constructing a VCL control, you may be tempted to have the form (or any other control) own it, but that actually puts additional overhead on the construction and destruction. If possible you should explicitly free it, this is especially true if you put the control on once, then you know you will be freeing it in your form's destructor or when it closes. The only time you can't do this is if the control creation is more dynamic.

所以是的,最佳实践是使用很多try / finally块.您应该只有几个try / except块,并且大多数都应该捕获非常特定的异常类型,并且/或者重新引发异常.如果try / except个块多于try / finally个块,则 您做错了 .

So yes, it is a best practice to use a lot of try / finally blocks. You should only have a few try / except blocks, and most all of them should trap very specific exception types, and / or re-raise the exception. If you have more try / except than try / finally blocks, then you are doing it wrong.

这篇关于我应该在每个Object.Create之后放置一个try-finally块吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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