Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using New/Dispose with record pointer containing WideString

I have very old code (from D3):

TMyRecord  = record
  Index   : Integer;
  Header  : String[70];
  Strings : Array[1..MAX_VALUES] of String[70];
end;

TMyClass = class(TComponent)
  FData  : ^TMyRecord;
  ...
end;

constructor TMyClass.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  New(FData);     
  ...
end;

destructor TMyClass.Destroy;
begin
   Dispose(FData);
   inherited;
end;

Q: Is it safe to replace String[70] with WideString; and Array[1..MAX_VALUES] of String[70] with Array[1..MAX_VALUES] of WideString? (Please explain why)

I need this to support Unicode in Delphi 7.

like image 958
zig Avatar asked Jun 10 '16 08:06

zig


1 Answers

In general you should never use Widestring. It is only meant for compatibility with COM BSTR.

However you are using a pre-2009 version so if you need Unicode you don't have a choice.
WideString is dynamically allocated, when you new the record Delphi will add code to initialize your strings.
You don't have to initialize them yourself.

Just like shortstrings WideStrings are not reference counted, but they will be destroyed when you dispose the record.
If you assign the Widestring to another Widestring Delphi will make a copy, this is slightly less efficient than refcounting, but otherwise not a problem.

Whenever a Widestring goes out of scope it is destroyed.

Be careful with PWideChar, these will be dangling when a WideString is destroyed.

VCL cannot display WideString
Note that whilst Delphi 7 does have support for Unicode with Widestring the VCL cannot display your Widestrings, it can only display AnsiString.
If you want to display WideStrings use the TNT components, see this answer for more info: Handling a Unicode String in Delphi Versions <= 2007

If you are going to assign WideString to (Ansi)string you might as well use plain string, because you'll lose all your unicode.
You can use UTF8, but D7 cannot display UTF8 either.

Caveat: indexing in Asian locales
A further caveat is that MyWidestring[i] does not necessarily mean the ith character because Unicode cannot be fully expressed in 2 bytes per char.
Unless you're using an asian language this should not affect you.

Q: Is it safe to replace String[70] with WideString;

Yes, but it is easier to replace String[70] with String (aka AnsiString). Because the D7 VCL supports AnsiString but not WideString.
Other than that you don't really have a problem.

Further reading
https://grahamwideman.wikispaces.com/Delphi+String+Types

like image 74
Johan Avatar answered Sep 19 '22 19:09

Johan