FreeAndNil: what changed
Revision 1 to 2. Removed lines have a minus sign; added lines have a plus sign.
| Old | New | Line | |
|---|---|---|---|
| 1 | 1 | **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]]. | |
| 2 | 2 | ||
| 3 | 3 | ``` pascal | |
| 4 | 4 | procedure FreeAndNil( var obj ); | |
| 5 | 5 | ``` | |
| 6 | 6 | ||
| 7 | 7 | Example: | |
| 8 | 8 | ||
| 9 | 9 | ``` pascal | |
| 10 | 10 | {$mode ObjFPC} | |
| 11 | 11 | uses SysUtils; | |
| 12 | 12 | ||
| 13 | 13 | type | |
| 14 | 14 | SomeClass = class(TObject) | |
| 15 | 15 | destructor Destroy; override; | |
| 16 | 16 | end; | |
| 17 | 17 | ||
| 18 | 18 | destructor SomeClass.Destroy; | |
| 19 | 19 | begin | |
| 20 | 20 | WriteLn('SomeClass Destructor called'); | |
| 21 | 21 | inherited; | |
| 22 | 22 | end; | |
| 23 | 23 | ||
| 24 | 24 | var | |
| 25 | 25 | myClass : SomeClass; | |
| 26 | 26 | myClass2: SomeClass; | |
| 27 | 27 | begin | |
| 28 | 28 | myClass := SomeClass.Create; | |
| 29 | 29 | WriteLn('myClass is assigned? ', Assigned(myClass)); | |
| 30 | 30 | myClass2 := SomeClass.Create; | |
| 31 | 31 | WriteLn('myClass2 is assigned? ', Assigned(myClass2)); | |
| 32 | 32 | myClass.Destroy; | |
| 33 | 33 | WriteLn('myClass is assigned? ', Assigned(myClass)); | |
| 34 | 34 | FreeAndNil(myClass2); | |
| 35 | 35 | WriteLn('myClass2 is assigned? ', Assigned(myClass2)); | |
| 36 | 36 | // assigning Nil after destructor is called is the same as | |
| 37 | 37 | // FreeAndNil | |
| 38 | 38 | myClass := Nil; | |
| 39 | 39 | WriteLn('myClass is assigned? ', Assigned(myClass)); | |
| 40 | 40 | end. | |
| 41 | 41 | ``` | |
| 42 | 42 | ||
| 43 | 43 | **Output:** | |
| 44 | 44 | ||
| 45 | - | ||
| 46 | - | ||
| 47 | - | ||
| 48 | - | ||
| 49 | - | ||
| 50 | - | ||
| 51 | - | ||
| 45 | + | ```text | |
| 46 | + | myClass is assigned? TRUE | |
| 47 | + | myClass2 is assigned? TRUE | |
| 48 | + | SomeClass Destructor called | |
| 49 | + | myClass is assigned? TRUE | |
| 50 | + | SomeClass Destructor called | |
| 51 | + | myClass2 is assigned? FALSE | |
| 52 | + | myClass is assigned? FALSE | |
| 53 | + | ``` | |
| 52 | 54 | ||
| 53 | 55 | --- | |
| 54 | 56 | *From the Free Pascal wiki, page "FreeAndNil", as it stood on 2020-12-27 after a change by Jouborg, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).* | |
| 55 | 57 | Category: FPC |
