Name
Finalize Procedure
Syntax
procedure Finalize(var Buffer); procedure Finalize(var Buffer; Count: Integer);
Description
The Finalize
procedure cleans up strings, dynamic
arrays, interfaces, Variants, and records or
arrays that contain these types. Delphi automatically finalizes
variables of string, dynamic array, interface, or
Variant type, but if you allocate such types
dynamically, you need to finalize the memory before freeing
it.
If you are finalizing more than one item in an array, pass the count
of the number of array elements as the Count
parameter. The Count is the number of array
elements, not the number of bytes to be freed.
Finalize is not a real procedure.
Tips and Tricks
DisposecallsFinalizefor you, so you need to callFinalizeonly when you free memory by callingFreeMem.The first argument to
Finalizeis not a pointer, but the actual variable or dereferenced pointer.Finalizemust know the type of the buffer so it can determine how to finalize array and record members. If you are casting a genericPointer, be sure you cast it to the correct type. Typecasting pointers is a common source of hard-to-locate bugs.
Example
type
TSample = record
Str: string;
List: array of Integer;
Intf: IUnknown;
V: Variant;
end;
TSampleArray = array[0..MaxInt div SizeOf(TSample)-1] of TSample;
PSampleArray = ^TSampleArray;
// See the Initialize procedure to see how to allocate a TSample array.
procedure FreeSamples(Samples: PSampleArray; Count: Integer);
begin
Finalize(Samples^, Count); ...Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Read now
Unlock full access