Question

procedure TForm1.UDPUDPRead(AThread: TIdUDPListenerThread; 
                              AData: array of Byte; ABinding: TIdSocketHandle);
var
  buffer : TBytes;
begin
  SetLength(buffer, Length(AData));
  buffer := @AData[0];
 end;

This code results in an access violation.

What would be the proper way to convert from array of byte to TBytes in Delphi XE3?

Was it helpful?

Solution

You need to copy the buffer.

Count := Length(AData);
SetLength(buffer, Count);
if Count <> 0 then
  Move(AData[0], buffer[0], Length(AData));

I have a feeling that this part of Indy was screwed up by Embarcadero. Note the dubious passing of array by value. If I recall, the version on Indy from the repo is better.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top