Name
Finalize Procedure
Syntax
procedure Finalize(var Buffer); procedure Finalize(var Buffer; Count: Integer);
Description
The Finalize
procedure cleans up strings, dynamic
arrays, interfaces, Variant
s, 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
Dispose
callsFinalize
for you, so you need to callFinalize
only when you free memory by callingFreeMem
.The first argument to
Finalize
is not a pointer, but the actual variable or dereferenced pointer.Finalize
must 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); ...
Get Delphi in a Nutshell now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.