System.SysUtils.FreeAndNil
Delphi
procedure FreeAndNil(const [ref] Obj: TObject);
Properties
Type | Visibility | Source | Unit | Parent |
---|---|---|---|---|
procedure | public | System.SysUtils.pas | System.SysUtils | System.SysUtils |
Description
Frees an object reference and replaces the reference with nil (Delphi) or NULL (C++).
Use FreeAndNil to ensure that a variable is nil (Delphi) or NULL (C++) after you free the object it references. Pass any variable that represents an object as the Obj parameter.
Warning: Obj must be an instance of a TObject descendant.
C++ now offers two templates implementation of FreeAndNil that allow both, the older as well as the newer and better, usage patterns. The older usage is when you had to pass the address of a delphi-style class pointer, like this:
TButton* button1;
....
FreeAndNil(&button1);
RAD Studio 10.4 now allows you to pass a reference to a delphi-style class pointer, like this:
TButton *button1 = <initialization>;
....
FreeAndNil(button1); // No need to take the address of 'button1'
Note: The logic uses a it means that FreeAndNil was invoked with an invalid parameter.
static_assert
to ensure that the parameter pass to FreeAndNil as a pointer to a type that derives from TObject. If you see the compiler error: "'T' must derive from System::TObject"
See Also
Code Examples