HomeDelphiDelphi Error – E2154 Type ‘%s’ needs finalization – not allowed in variant record

Delphi Error – E2154 Type ‘%s’ needs finalization – not allowed in variant record

Delphi Compiler Error

E2154 Type ‘%s’ needs finalization – not allowed in variant record

Reason for the Error & Solution

Certain types are treated specially by the compiler on an internal basis in that they must be correctly finalized to release any resources that they might currently own. Because the compiler cannot determine what type is actually stored in a record’s variant section at run time, it is not possible to guarantee that these special data types are correctly finalized.

program Produce;

  type
    Data = record
      case kind:Char of
      'A': (str : String);
    end;

begin
end.

String is one of those types which requires special treatment by the compiler to correctly release the resources. As such, it is illegal to have a String in a variant section.

program Solve;

  type
    Data = record
      str : String;
    end;

begin
end.

One solution to this error is to move all offending declarations out of the variant section. Another solution would be to use pointer types (^String, for example) and manage the memory by yourself.

Share:

Leave A Reply

Your email address will not be published. Required fields are marked *

You May Also Like

Delphi Compiler Error X2421 Imported identifier ‘%s’ conflicts with ‘%s’ in ‘%s’ Reason for the Error & Solution This occurs...
Delphi Compiler Error X2367 Case of property accessor method %s.%s should be %s.%s Reason for the Error & Solution No...
Delphi Compiler Error X2269 Overriding virtual method ‘%s.%s’ has lower visibility (%s) than base class ‘%s’ (%s) Reason for the...