Difference between revisions of "FreeAndNil"

From Free Pascal wiki
Jump to navigationJump to search
(Example)
m
 
(2 intermediate revisions by 2 users not shown)
Line 1: Line 1:
'''FreeAndNil''' is a procedure defined in the [[SysUtils unit]] of the Free Pascal [[RTL|Runtime Library]]. It calls an object's constructor (via TObject.Free) and also sets the object variable (which is a reference to heap memory created by a constructor call) to nil. If you just call an object's destructor, [[Assigned]] will still return [[True]] for the object variable. But after calling FreeAndNil, Assigned will return [[False]] for the object variable. While the procedure has an untyped parameter, it is only for use with object references - that is variables that are instances of a [[Class]].
+
'''FreeAndNil''' is a procedure defined in the [[SysUtils unit]] of the Free Pascal [[RTL|Runtime Library]]. It calls an object's destructor (via TObject.Free) and also sets the object variable (which is a reference to heap memory created by a constructor call) to nil. If you just call an object's destructor, [[Assigned]] will still return [[True]] for the object variable. But after calling FreeAndNil, [[Assigned]] will return [[False]] for the object variable. While the procedure has an ''untyped parameter'', it is only for use with object references - that is variables that are instances of a [[Class]].
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
procedure FreeAndNil( var obj );
 
procedure FreeAndNil( var obj );
 
</syntaxhighlight>
 
</syntaxhighlight>
Line 7: Line 7:
 
Example:
 
Example:
  
<syntaxhighlight>
+
<syntaxhighlight lang=pascal>
 
{$mode ObjFPC}
 
{$mode ObjFPC}
 
uses SysUtils;
 
uses SysUtils;
Line 41: Line 41:
 
</syntaxhighlight>
 
</syntaxhighlight>
  
'''Output:'''<br/>
+
'''Output:'''
myClass is assigned? TRUE<br/>
+
 
myClass2 is assigned? TRUE<br/>
+
  myClass is assigned? TRUE
SomeClass Destructor called<br/>
+
  myClass2 is assigned? TRUE
myClass is assigned? TRUE<br/>
+
  SomeClass Destructor called
SomeClass Destructor called<br/>
+
  myClass is assigned? TRUE
myClass2 is assigned? FALSE<br/>
+
  SomeClass Destructor called
myClass is assigned? FALSE<br/>
+
  myClass2 is assigned? FALSE
 +
  myClass is assigned? FALSE
 +
 
 +
[[Category:FPC]]

Latest revision as of 19:59, 27 December 2020

FreeAndNil is a procedure defined in the SysUtils unit of the Free Pascal Runtime Library. It calls an object's destructor (via TObject.Free) and also sets the object variable (which is a reference to heap memory created by a constructor call) to nil. If you just call an object's destructor, Assigned will still return True for the object variable. But after calling FreeAndNil, Assigned will return False for the object variable. While the procedure has an untyped parameter, it is only for use with object references - that is variables that are instances of a Class.

procedure FreeAndNil( var obj );

Example:

{$mode ObjFPC}
uses SysUtils;

type
   SomeClass = class(TObject)
      destructor Destroy; override;
   end;

destructor SomeClass.Destroy;
begin
   WriteLn('SomeClass Destructor called');
   inherited;
end;

var
   myClass : SomeClass;
   myClass2: SomeClass;
begin
   myClass  := SomeClass.Create;
   WriteLn('myClass is assigned? ', Assigned(myClass));
   myClass2 := SomeClass.Create;
   WriteLn('myClass2 is assigned? ', Assigned(myClass2));
   myClass.Destroy;
   WriteLn('myClass is assigned? ', Assigned(myClass));
   FreeAndNil(myClass2);
   WriteLn('myClass2 is assigned? ', Assigned(myClass2));
   // assigning Nil after destructor is called is the same as
   // FreeAndNil
   myClass := Nil;
   WriteLn('myClass is assigned? ', Assigned(myClass));
end.

Output:

 myClass is assigned? TRUE  
 myClass2 is assigned? TRUE
 SomeClass Destructor called
 myClass is assigned? TRUE
 SomeClass Destructor called
 myClass2 is assigned? FALSE
 myClass is assigned? FALSE