The FreeAndNil procedure frees up the memory used by an object, and sets the object reference to nil.
It actually does this in reverse order - first dereferencing the object before deallocating the memory. This is a very clean way of freeing resources.
|Free memory storage used by a variable
|Get a specified number of storage bytes
|A pointer value that is defined as undetermined
|A variable that has no value
| Download this web site as a Windows program.
|Example code : Free and nil an object, and then try to do this again
myList : TList;
// Create the list object
myList := TList.Create;
// And now free and nil this object
// We can safely do this twice - it ignores nil objects
|Show full unit code
| No exception occurs - the second FreeAndNil does nothing.