Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Deactivate Huge Strings in Delphi 2010

I'm new in Delphi and I was migrating a very old Delphi project to Embarcadero RAD Studio 2010. I found a problem using strings. Here is the code:

ProgramaResultadosType = record
    Version: string;
    TituloPrincipal   : string;
    BloquesResultados : VectorBloquesResultadosType;
end;

FileOfProgramaResultadosType  = file of ProgramaResultadosType;

And the error...

"E2155 Type '%s' needs finalization - not allowed in file type"

I know it's a well-known error for many delphi developers when you don't specify the string size.

Basically I would like to deactivate Huge strings directive like older versions of RAD Studio, but I can't find out in the 2010 version.

like image 656
MJ-79 Avatar asked Apr 20 '12 02:04

MJ-79


1 Answers

Just use instead:

ProgramaResultadosType = record
    Version: shortstring;
    TituloPrincipal   : shortstring;
    BloquesResultados : VectorBloquesResultadosType;
end;

But be aware that:

  • Almost every call to a RTL function will first make an invisible conversion to the new string type: you should better use another explicit string variable just after having read the shortstring content;
  • In newer version of Delphi (starting with Delphi 2009), string is an Unicode string: so you may lose some data when writing into a shortstring (which is an Ansi string) from an UnicodeString;
  • Perhaps adding ProgramaResultadosType = packed record could be necessary if your application is very very old (default alignment changed around Delphi 4, AFAIR).

So you may have to:

  • Read the existing content using structures like the above;
  • Write using a new paradigm, Unicode ready (e.g. an embedded DB like Midas, SQLite3 or any other).

Worth reading when converting an existing application to newer Unicode version of Delphi:

  • Series of articles;
  • Presentation by Marco;
  • Delphi Unicode Migration for Mere Mortals: Stories and Advice from the Front Lines.
like image 80
Arnaud Bouchez Avatar answered Oct 08 '22 07:10

Arnaud Bouchez