Release,Destroy和Free的区别(100分)

  • 主题发起人 主题发起人 cctime
  • 开始时间 开始时间
free在释放对象前检查一下是否为nil,如果不是才destroy, release就不知道了
 
sanke说得对,所以你最好不要直接调用Destroy,用Free才安全。如果是释放窗体,
那么最好用Release。Borland解释如下:

Use Free to destroy an object. Free automatically calls the Destroy if the
object instance is not nil. Any object instantiated at runtime that does not
have an Owner should be destroyed by a call to Free, so that the object can
be properly destroyed and the memory released. Free is successful even if the
object is nil, so if the object was never initialized, for example, calling
Free won抰 result in an error.

When you call Free for a component, it will call Free for all components that
it owns, that is, all components in its component list. A form owns all the
controls and non-visual components that are created on it in design mode.
When it is freed, all of these components are automatically freed as well.
Since, by default, all forms are owned by the Application object, when the
application terminates, it frees the Application object, which frees all forms.
For all objects that are not components, or for components created by a nil
owner, be sure to call Free after you are finished with the object
otherwise
the memory allocated to the object will not be usable until after the
application terminates.

You should never explicitly free a component within one of its own event
handlers, nor should you free a component from an event handler of a
component the component owns or contains. For example, you should avoid
freeing a button in its OnClick event handler. Nor should you free the
form that owns the button from the button's OnClick event.

If you want to free the form, call the Release method, which destroys the
form and releases the memory allocated for it after all its event handlers
and those of the components it contains are through executing.
 
Destroy 是析构函数,不能这样调用释放对象。
Free 是标准用法,用于在外部释放对象。
Release 主要用于 Form 对象。用于在 Form 对象本身的方法中释放自身,在 Form 对象本身的方法
中不要使用 Free ,否则会引起麻烦。
 
多人接受答案了。
 
后退
顶部