Do I have to be free after create?
Simple Example
Procedure;
VaR X: TX;
Begin
X: = Tx. Create;
// Do someting;
X. Free // if I am not free here, isn't this end equivalent to} Auto Release in C?
That is to say, this free option is redundant here. Right?
End;
But for reliability and readability, it is best not to save this free space.
And use try... finally... end to install it.
In this way, even if an exception occurs during the process, the resources can be released.
But for reliability and readability, it is best not to save this free space. And use try... finally... end to install it. In this way, even if an exception occurs during the process, the resources can be released. |
But for reliability and readability, it is best not to save this free space. And use try... finally... end to install it. In this way, even if an exception occurs during the process, the resources can be released. |
In this way, you must be free; otherwise, there will be a memory leak.
However, if X: = Tx. Create (Application) is not free, the application will be free for you when the application is free.
Create and release Delphi objects (Free/destory)
1. The create parameter is the difference between nil/self/application. It is best to see the difference in actual results.
Example: My: = tmy. Create (X );
X will become the owner property of the control my,
There may be more than one control that treats X as the owner.
When X is released, it also releases all controls that regard it as the owner.
For example:
Btn1: = tbutton. Create (Panel1 );
Btn2: = tbutton. Create (Panel1 );
Btn3: = tbutton. Create (Panel1 );
When panel1.free is enabled, both btn1, btn2, and btn3 are released.
If btn1: = tbutton. Create (Self); {for example, self is a form}
When the form is released, btn1 will also be released.
Btn1: = tbutton. Create (application );
When the program is destroyed, btn1.
This is basically the same for a single form program, but it is different for multiple forms.
If btn1: = tbutton. Create (NiL );
You have to manually release btn1 (btn1.free ).
You may ask: if the form in which btn1 is located is destroyed, Will btn1 still exist?
A: If the application is still in btn1, it will not disappear (in the case of create (NiL ).
Generally, when the application is absent, the system releases the control contained in the application.
However, it is not a good habit to wait for the system to do this, because there are exceptions (usually cross-process), such a sentence:
The memory allocated by an object may be longer than the existing time of the object.
2. There are two methods to log out of Delphi: destroy and free.
Delphi recommends using free because it is safer than destroy, and calling free will generate more efficient code.
You can use the following statement to release the used employee object:
Employee. Free;
Like the create method, the free method is inherited by temployee from tobject. Put your logout in the finally part of the tryfinally program module,
Put the program code of the object in the try part is a good habit of programming. In this way, even if your program code encounters an exception when using the object,
It will also ensure that the memory you allocate to this object will be released.
The difference between the two is that destroy will directly release the object,
Free checks whether the object exists. If the object exists, or the object is not nil, it calls destroy.
Therefore, the program should try to use free to release objects, which is more secure.
(Note that free does not automatically set the object to nil. Therefore, it is best to manually set the object to nil after free is called .)
The tobject class has a virtual destroy fictitious function and a non-virtual free function. In the free function, destroy is called.
Therefore, when we call. Free (); for any object (a subclass object of tobject), we will execute tobject. Free ();
It will call the destroy (); destructor of the object we use ();. This ensures that all types of objects can be correctly parsed.