Using New/Dispose with record pointer containing WideString

混江龙づ霸主 提交于 2019-12-10 14:03:33

问题


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.


回答1:


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



来源:https://stackoverflow.com/questions/37743076/using-new-dispose-with-record-pointer-containing-widestring

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!