Using New / Dispose with a record pointer containing a WideString

I have a 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] on Array[1..MAX_VALUES] of WideString ? (Please explain why)

I need this to support Unicode in Delphi 7.

+5
source share
1 answer

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

However, you use the version before 2009, so if you need Unicode, you have no choice.
WideString is dynamically allocated when you write Delphi new to add code to initialize strings.
You do not need to initialize them yourself.

Just like shortstrings, WideStrings do not count, but they will be destroyed when you dispose .
If you assign Widestring to another Widestring, Delphi will make a copy, it is a little less efficient than refcounting, but otherwise it is not a problem.

Whenever a Widestring goes out of scope, it is destroyed.

Be careful with PWideChar, they will hang out when destroying WideString.

VCL cannot display WideString
Note that while Delphi 7 has Unicode support with Widestring, VCL cannot display your Widestrings, it can only display AnsiString.
If you want to display WideStrings, use TNT components, see this answer for more information: Unicode string handling in Delphi versions <= 2007

If you are going to assign a WideString (Ansi) string, you can use a regular string because you will lose all your Unicode.
You can use UTF8, but D7 also cannot display UTF8.

Caution: Indexing in Asian Regions
Another caveat is that MyWidestring[i] does not necessarily mean the ith character, because Unicode cannot be fully expressed in 2 bytes per char.
If you do not use Asian, this should not affect you.

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

Yes, but replacing String[70] with String (aka AnsiString) is easier. Because the D7 VCL supports AnsiString, but not WideString.
Other than that, you really have no problem.

additional literature
https://grahamwideman.wikispaces.com/Delphi+String+Types

+7
source

All Articles