Delphi Error – E2155 Type ‘%s’ needs finalization – not allowed in file type

Delphi Compiler Error

E2155 Type ‘%s’ needs finalization – not allowed in file type

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
      name : string;
    end;

  var
    inFile : file of Data;

begin
end.

String is one of those data types which need finalization, and as such they cannot be stored in a File type.

program Solve;

  type
    Data = record
      name : array [1..25] of Char;
    end;

  var
    inFile : file of Data;

begin
end.

One simple solution, for the case of String, is to redeclare the type as an array of characters. For other cases which require finalization, it becomes increasingly difficult to maintain a binary file structure with standard Pascal features, such as ‘file of’. In these situations, it is probably easier to write specialized file I/O routines.

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...