In 2001 I published an article, named "Exploring: How to determine if the object pointer in Delphi is available". In the text, an access or calling of an attribute / method that has been released by the object is proposed, and then the object pointer is available for use in whether an abnormality is displayed.
This method is ridiculous! I want to help you make a wise judgment with the operation of the following procedures.
Unit unit1;
Interface
Uses Windows, Messages, Sysutils, Classes, Graphics, Controls, Forms, Dialogs, Stdctrls;
type TForm1 = class (TForm) btn1: TButton; btn2: TButton; btn3: TButton; procedure btn1Click (Sender: TObject); procedure btn2Click (Sender: TObject); procedure btn3Click (Sender: TObject); private {Private declarations} public { Public Declarations}
Var Form1: TFORM1;
IMPLEMentation
{$ R * .dfm}
Procedure tForm1.btn1click (Sender: Tobject); var A: tcomponent; s: string; begin // do create a comonent and free. A: = tcomponent.create (self); a.name: = 'a_name'; a .TAG: = 1111; s: = a.getnamepath; a.free;
// try to access object's method or proPery. Assert (a.name <> 'a_name'); // Established! The reason is that after the string is released, its length becomes zero, so it is an empty string assert (a.name = ''); // is established! Assert (a.tag = 1111); // is established! Assert (a.componentstate = [csdestroying, csancestor]); // is established! A.FindComponent (''); // Pass! The inside has an object access to access. A.Name: = 'newname'; // Abnormal! Exception.name = EACCESSVIOLATION; Exception.Message: 'Access Violation at address 0000000. read of address 00000000' a.getParentComponent; // Exception! The code stays in Sytem.getDynamethod. Access Violation ... read of address 00000007. "Assert (A.GetParentComponent = Self); // Exception! The code stays in Sytem.getDynamethod. Access Violation ... read of address 00000007. "assert (A.GetNamePath = S); // Exception! The code stays in Sytem.getDynamethod. "Access Violation ... Read of address 00000007." end; procedure tform1.btn2click (sender: Tobject); VAR A, B: Tcomponent; begin // do create a comonent and free it. A: = tComponent.create (Self ); A.Name: = 'a_name'; a.tag: = 1111; a.free;
// assert it. B: = tcomponent.create (self); b.Name: = 'b_name'; b.tag: = 2222; try assert (a = b); // Established! why? Finally B.free; end;
Procedure TFORM1.BTN3Click (Sender: TOBJECT); VAR A, B, C: Tcomponent; s: string; begin a: = tcomponent.create (self); a.name: = 'a_name'; a.tag: = 1111; S: = a.getnamepath; a.free;
C: = TButton.create (Self);
B: = Tcomponent.create (Self); B.Name: = 'b_name'; b.tag: = 2222;
Try assert (a <> b); assert (a.name <> 'a_name'); // is established! It is equal to the original value! ASSERT (a.name <> '); // Established! Reason: The possibility is extremely small, but it is not possible. CAPTION: = a.name; // caption is a pile of garbled! ASSERT (a.tag <> 1111); // is established! CAPTION: = INTOSTR (a.tag); // caption is a random integer. Assert (a.componentstate = []); // is established! A.FindComponent (''); // Pass! The inside has an object access to access. A.Name: = 'newname'; // Abnormal! Stay in Controls.Procedure Tcontrol.click; Exception.name = EACCESSVIOLATION; Exception.Message: 'Access Violation At Address 00200041. Read of Address 00200041' a.getParentComponent; // Exception! EabstracTerror. Stop in the Procedure Abstracterrorhandler of the Sysutils unit; Similar to the above, read of address 00000007. assert (A.GetNamePath <> s); // passed! Caption: = a.getnamepath; // caption is 'tfont'. why? Finally B.free; C.free; end; end; end.
In summary, the subsequent use of the object pointer depends on the specific behavior of the program after the object is released. If there is an object of the same type, it is likely to be the same. If any objects are not created later, and the partial object method or attribute has a specific value. If subsequent memory has some different changes, the results are different. Its mechanism is the issue of Delphi's creation and destruction of the object - how beautiful is the judgment of how to make pointers can be used to use some way to make pointers.