I am porting my code from 10.1 to 10.2 and this gives me error:
procedure TForm4.FormCreate(Sender: TObject);
const
CFourBytes: array[0..3] of Byte = (1, 2, 3, 4);
var
LStream: TMemoryStream;
LBuffer: array of Byte;
begin
SetLength(LBuffer, 4);
LStream := TMemoryStream.Create;
LStream.Write(@CFourBytes[0], 4); // E2036 Variable required
LStream.Position := 0;
LStream.ReadData(@LBuffer[0], 4);
end;
I had to change offending line to LStream.Write(CFourBytes[0], 4);
What has changed? Have I been doing it wrong for the whole time?
The code in your question did compile in older versions, but it should not have done. The behaviour seen in 10.2 is correct.
What happens in old versions is very strange. The compiler selects this overload in
TStream:That is especially egregious because what has been passed to this method is the address of the static array
CFourBytes. Which is categorically not aTBytesobject.Now it just so happens that a
TBytesvariable is the address of the first element of the array. And nothing in theTBytesoverride forTMemoryStream.Writerefers toLength()of that bogusTBytesobject. So your code happens to work as intended. This is very clearly a compiler error that has been fixed.Your code has always been broken, you have just, up until now, been getting away with it by fortune. You should fix your code. Like this:
Note that I am using
WriteBufferandReadBufferinstead ofWriteandRead. These are the preferred methods to use withTStream. The reason being that they perform error checking and raise exceptions in case of errors, unlikeWriteandRead.