Delphi in Destroy, Free, Freeandnil, release usage and differences

Source: Internet
Author: User

Delphi in Destroy, Free, Freeandnil, release usage and differences

1) Destroy: Virtual method

Frees the memory, declares it as virtual in tobject, usually override it in its subclass, and adds the inherited keyword to ensure that the derived class object is properly destroyed;
But destroy generally can't use directly, why?
When an object is nil, we still call destroy, which produces an error. Because destroy is a virtual method, it will find the entry address of the virtual method table VMT based on the first four bytes in the object, thus finding the entry address of the destroy, so the object must exist at this time. However, free is a static method, which is determined by the type of object reference/pointer, even if the object itself does not exist and does not produce an exception, and there is an operation to determine the existence of the object, so in general, the object is freed.

2) Free: Static method

Test whether the object is nil and not nil to invoke destroy. The following is the Delphi code for free:
Procedure Tobject.free;
Begin
If self <> nil then
Destroy;
End
A quiet move, learn from each other, not wonderful!
But the destroy of the calling object only destroys the object, but does not set the object's reference to nil, which needs to be done by the programmer, but since Delphi5, a FREEANDNIL has been provided in the Sysutils unit.

3) Freeandnil: A standalone function that does not belong to any object, non-object method, non-class method.

Procedure Freeandnil (var Obj);
Var
Temp:tobject;
Begin
Temp: = TObject (OBJ);
Pointer (OBJ): = nil;
Temp.free;
End

It is recommended that you use it instead of Free/destroy in order to ensure that the object is disposed correctly.

4) Static methods defined in the Release:tcustomform.

The free function is called only after all the events in the window have been processed. It is often used in destroying windows, and in this window when event processing takes a certain amount of time, this method ensures that the window is not destroyed until the window event is processed.
Here is the Delphi source code for Tcustomform.release:
Procedure Tcustomform.release;
Begin
PostMessage (Handle, cm_release, 0, 0);
Sends a CM_RELEASE message to the window to the message queue, when all the window event messages have been processed,
Call the Cm_release message processing procedure again Cmrelease
End
Take a look at the following cm_release message processing procedure cmrelease definition:
Procedure Cmrelease (var message:tmessage); Message cm_release;
Procedure Tcustomform.cmrelease;
Begin
Free; Finally, it's free;
End

Delphi in Destroy, Free, Freeandnil, release usage and differences

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.