I am having the same problem as mentioned in "Delphi XE4 Indy compatibility issue between TBytes and TidBytes ", i.e. compatibility issues between TBytes(Delphi RTL) and TIdBytes(Indy) datatypes when compiling with the Delphi XE4. The source of my problem is that the code is not exactly according to Indy's interface and some of functions use TBytes, instead of TIdBytes, when calling native Indy IO procedures.
So I was wondering what will the best fix be?
As I see it there are two approaches:
Refactor all functions in the project to use TIdBytes rather than TBytes.
Implement a TBytesToTidBytes conversion procedure (converts the TBytes to TIdBytes) and call that procedure prior to making the mentioned native Indy calls.
Which of the approaches is better/best? Do you have any other ideas on how I can do that?
FYI: The project I am trying to configure with the XE4 is available online on sourceforge : http://sourceforge.net/projects/indy10clieservr/?source=directory
The suggested conversion procedure should be something like:
procedure TBytesToTIdBytes(const Input:TBytes, var Output: TIdBytes)
var
i,L : Integer;
allocate : Boolean;
begin
L := Length(Input);
if(Length(Output) <> L) then
begin
SetLength(Output,L);
end;
if(L > 0) then
move(Pointer(Input)^,Pointer(Output)^,L);
end;
allocate
variable serves no purpose, but your length check is dangerous. If the arrays have the same length, then you won't reallocate the array, but if the target array has a reference count greater than one, you'll end up overwriting an array you might not intend to. Code holding the other reference to the array might continue expecting the original data. It's safer to reallocate unconditionally. – FatnessSetLength
, that call toSetLength
has no net effect. The only time the value ofallocate
makes you callSetLength
is when it's true. The only wayallocate
is true is whenOutput
is null, which means it's an empty array. IfInput
is an empty array, then you didn't need to callSetLength
because it will just makeOutput
empty, which we know it already was. IfInput
isn't empty, then the length comparison would have evaluated to true anyway, even without checking the value ofallocate
. – Fatness