Initialize Delphi variables according to storage and type
Do not infer a Delphi variable's initial value from a successful debug run. Initialization depends on storage and type. Ordinary local integers, pointers and object references must be assigned before they are read. Compiler-managed values have initialization and finalization support, while object fields and global storage begin zero-initialized.
This distinction matters during cleanup. Calling Free on a local object variable that never received a value is unsafe. Assign nil before entering a cleanup path that might run before construction, or create the object before the corresponding try/finally.
Make the ordinary values explicit
Create a Delphi 13 console application targeting Windows. Replace its project source with this complete program. It does not read an uninitialized scalar to โdiscoverโ what happens.
program InitializationDemo;
{$APPTYPE CONSOLE}
uses
System.SysUtils;
type
TSettings = record
Count: Integer;
Name: string;
end;
TCounter = class
public
Count: Integer;
end;
var
GlobalCount: Integer;
procedure Run;
var
LocalCount: Integer;
LocalObject: TObject;
Text: string;
Values: TArray<Integer>;
Settings: TSettings;
Counter: TCounter;
begin
LocalCount := 42;
LocalObject := nil;
Settings := Default(TSettings);
Counter := TCounter.Create;
try
Writeln('Global=', GlobalCount);
Writeln('Field=', Counter.Count);
Writeln('Local=', LocalCount);
Writeln('Object nil=', BoolToStr(LocalObject = nil, True));
Writeln('Managed string length=', Length(Text));
Writeln('Managed array length=', Length(Values));
Writeln('Record count=', Settings.Count);
finally
Counter.Free;
end;
end;
begin
Run;
end.
Expected output:
Global=0
Field=0
Local=42
Object nil=True
Managed string length=0
Managed array length=0
Record count=0
The local string and dynamic array are managed types. The compiler prepares them for safe use and cleanup. LocalObject is an ordinary class reference, so the program assigns nil explicitly. Constructing TCounter initializes its instance fields, which is why Counter.Count begins at zero.
Use the right initialization mechanism
| Value | Initial-state rule |
| Ordinary local integer, pointer or object reference | Assign before reading |
| Global variable and ordinary object instance field | Zero-initialized storage |
| Local string, dynamic array or interface | Compiler-managed initialization and cleanup |
| Plain record containing ordinary scalar fields | Initialize the whole record or each required field |
| Custom managed record | Follow its Initialize, Finalize and Assign contract |
| Raw memory from GetMem | Treat contents as uninitialized |
The sample uses Default(TSettings) to establish a complete default value for an ordinary record. Do not assume the managed string field causes every unrelated scalar field in every local record to have the intended business default. Assign those defaults explicitly.
Avoid clearing an existing managed record with FillChar. Overwriting live string, interface or dynamic-array references bypasses their cleanup and can leak resources or violate invariants. Byte clearing is appropriate only when the target's representation and lifecycle permit it, such as a plain Win32 structure with no managed fields.
Separate initialization from ownership
An initialized nil reference does not own an object. A non-nil class reference does not automatically free its instance when the local variable leaves scope. Use explicit ownership and try/finally, or the ownership mechanism already defined by the containing framework.
Interfaces are managed references, but their implementation determines the actual reference-counting behavior. Components can also have an owner that frees them. Avoid freeing the same object through two independent ownership paths.
Compiler warnings about potentially uninitialized values deserve investigation. Optimized builds can reuse storage differently from debug builds, so observing zero once is not a guarantee. Test every branch that reaches a read or cleanup operation, including exceptions during partial construction.
References
Question provenance
This article addresses Are delphi variables initialized with a value by default? , asked by MB. on Stack Overflow. The source question is licensed under CC BY-SA 3.0 . The explanation and examples here are newly authored for current Delphi. No answer text or code is reproduced.
Author: Herman Schoenfeld