[Lazarus] Dangers of using Move command to insert data into a TBytes array?
Bo Berglund
bo.berglund at gmail.com
Fri Sep 1 07:35:15 CEST 2017
On Wed, 30 Aug 2017 09:48:47 +0200 (CEST), Michael Van Canneyt via
Lazarus <lazarus at lists.lazarus-ide.org> wrote:
>> I must use Move() for handling other data of various kinds like packed
>> records and such too...
>
>If the packed records contain managed data (ansistrings and the like):
>Don't use move.
Thanks for the advice!
I now have looked over the code and found that the records used are
fixed length, but there exist in some places a few ID members that are
supposed to hold an array of AnsiChar, like in this example:
TSSCmdDirRec = packed record
CmdProgNum: shortint;
InUse: shortint;
ProgID: array[0..16] of AnsiChar; // <== here!
meastype: byte;
numSteps: word;
MUX: shortint;
numElecs: word;
progLoc: cardinal;
ArrayID: shortint;
end;
These records are equivalents of the C structs used on an embedded
data collection system, which sends data organized in this way in
straight binary form. In the case of ProgID it is defined there as:
char progID[17];
What I do is that I receive the binary data into a buffer, then use
Move() to copy the data into a local varable of the record type and
finally I use a procedure to switch endianness on members which are
multi-byte.
This has worked fine until Unicode was introduced in Delphi/FPC when I
tried to fix it using AnsiChar and AnsiString as appropriate.
But no string type in these records, just arrays of AnsiChar...
Anyway, I'm glad I checked with you. I initially had calculated the
number of bytes in my Move operation erroneously. Now fixed.
I did not see any side effects of it though, probably just pure luck.
--
Bo Berglund
Developer in Sweden
More information about the Lazarus
mailing list