Recent

Author Topic: Strange, i cannot write to a stream, what gives?  (Read 609 times)

cdbc

  • Hero Member
  • *****
  • Posts: 1026
    • http://www.cdbc.dk
Strange, i cannot write to a stream, what gives?
« on: August 18, 2022, 01:25:35 pm »
Hi
I've got a very strange problem, a stream won't let me write to it?!?

I need some advice please.
Attached is a test project, to show what i mean...
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE5 -> FPC 3.2.2 -> Lazarus 2.2.6 up until Jan 2024 from then on it's: KDE5/QT5 -> FPC 3.3.1 -> Lazarus 3.0

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Strange, i cannot write to a stream, what gives?
« Reply #1 on: August 18, 2022, 01:39:06 pm »
It seems that the compiler picks the wrong overload of TStream.Read. You can work around this by using CopyFrom instead:

Code: Pascal  [Select][+][-]
  1. procedure TfrmConcStreams.ConcatenateStreams(aStream1: TMemoryStream;aStream2: TMemoryStream);
  2. const CrLf = #13#10'-----'#13#10;
  3. var
  4.   Buffer: TBytes;
  5.   Res: int64;
  6. begin
  7.   aStream2.Position := 0;
  8.   aStream1.Seek(0, soEnd);
  9.   aStream1.Write(CrLf, 9);
  10.   aStream1.CopyFrom(aStream2, aStream2.Size);
  11.   aStream1.Position := 0;
  12.   aStream2.Position := 0;  
  13. end;

Sidenote: you can use Length(CrLf) instead of hardcoding a 9 as well.

Edit: it seems that this is fixed in 3.3.1, maybe I can find the corresponding commit and merge it back to 3.2.3.
« Last Edit: August 18, 2022, 01:43:27 pm by PascalDragon »

ASerge

  • Hero Member
  • *****
  • Posts: 2223
Re: Strange, i cannot write to a stream, what gives?
« Reply #2 on: August 18, 2022, 08:32:08 pm »
It seems that the compiler picks the wrong overload of TStream.Read. You can work around this by using CopyFrom instead:
If FPC 3.2.2 TStream does not have overloaded Read, Write functions, in which the parameter has the type TBytes.
Therefore, need to use Read(buffer[0],...) instead of Read(buffer,...). The same with Write.

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Strange, i cannot write to a stream, what gives?
« Reply #3 on: August 19, 2022, 09:05:56 am »
It seems that the compiler picks the wrong overload of TStream.Read. You can work around this by using CopyFrom instead:
If FPC 3.2.2 TStream does not have overloaded Read, Write functions, in which the parameter has the type TBytes.
Therefore, need to use Read(buffer[0],...) instead of Read(buffer,...). The same with Write.

Ah, right! My IDE fooled me here, cause I have the sources set to ones from main despite using 3.2.2 as compiler. %)

@cdbc: do as ASerge said and then this will also work with newer versions of FPC. Or you can use my alternative with the CopyFrom (which will also work across versions).

 

TinyPortal © 2005-2018