The difference between Delphi objects and free the write-off method Destroy

When you are finished using the object, you should promptly withdraw it, in order to release the memory occupied by the object. You can call a logout method to undo your object, it releases the memory allocated to the object.

         Delphi write-off method has two: Destroy and Free. Delphi recommended Free, because it is more secure than Destroy, while calls will generate higher Free efficient code.

          You can run out of the release of the Employee object with the following statement: 

Employee.Free; 

          Create method and the same, Free TEmployee method is inherited from TObject in coming. Send your cancellation on the try finally ... finally part of the program modules, and the program code object in a try programming part is a good habit. In this way, even if your program code abnormal event occurs when using an object, it will ensure that you assign to the object memory will be released.

The difference is, Destroy will directly release the object, and the fact that Free will check whether the object exists, if there is an object, or object is not nil, it will be called Destroy. Therefore, the program should be  
     the make use of free to release the object, this is more secure. (But note that, free does not automatically set the target to nil, so after calling free, the best is to manually set the object nil.)
      TObject class fictional function and a non-virtual function of a virtual Destroy Free . Free Destroy the function is invoked. Thus, when we call .Free on any object (a subclass object is TObject) (); then, are performed TObject.Free () ;, which we will call the object used destructor Destroy () ;. This ensures that any type of object can correctly be destructed

Reproduced in: https: //www.cnblogs.com/rogee/archive/2010/09/20/1832003.html

Guess you like

Origin blog.csdn.net/weixin_34357436/article/details/94680824