Hello,
I would like to report an issue with the MakeDocumentFromStream function.
Steps to Reproduce:
1. Set the linewidth property to 0 (no automatic carriage return).
2. Write a single line with 2548 characters.
Observed Issue:
When reading the stream in MakeDocumentFromStream, the value of the Chunk variable during the second read is identical to the Buffer variable. As a result, the Buffer loses the first 1024 characters that were initially read.
Proposed Fix:
I resolved the issue (at least in my case) by replacing the following line:
Stream.ReadBuffer(PRVAnsiChar(Chunk)^, ChunkSize);
with:
Stream.ReadBuffer(Chunk[1], ChunkSize);
This seems to fix the iusse.
Best regards,
Alessandro Mancini
Out of memory in MakeDocumentFromStream
-
- Site Admin
- Posts: 17679
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Re: Out of memory in MakeDocumentFromStream
Sorry, I cannot reproduce the problem.
Do you use the newest version of TRichView?
I used this code:
This code shows '2548', as expected.
There is no "out of memory" message, and I cannot understand how it could appear here.
Data is not lost for long lines.
Iteration 1: Reading 1024 bytes (chunk#1) in Chunk, adding them to Buffer (Buffer contains chunk#1).
Iteration 2: Reading next 1024 bytes (chunk#2) in Chunk, adding them to Buffer (Buffer contains chunk#1+chunk#2)
Iteration 3: Reading last 500 bytes (chunk#3) in Chunk. Now the condition "if (LineLength >= 0) then" is True. Adding chunk#3 to Buffer (now it contains chunk#1+chunk#2+chunk#3) and calling ProcessLine(Buffer);
Everything works as expected.
The lines
Stream.ReadBuffer(PRVAnsiChar(Chunk)^, ChunkSize);
and
Stream.ReadBuffer(Chunk[1], ChunkSize);
are equivalent.
Do you use the newest version of TRichView?
I used this code:
Code: Select all
uses RVUni, RVStrFuncs;
var
S: TRVAnsiString;
Stream: TStream;
begin
S := RVMakeStringOfCharA('x', 2548);
Stream := RVU_AnsiStringToAnsiStream(S);
Stream.Position := 0;
RichViewEdit1.Clear;
RichViewEdit1.LoadMarkdownFromStream(Stream);
Stream.Free;
RichViewEdit1.Format;
ShowMessage(IntToStr(Length(RichViewEdit1.GetItemText(0))));
end;
There is no "out of memory" message, and I cannot understand how it could appear here.
Data is not lost for long lines.
Iteration 1: Reading 1024 bytes (chunk#1) in Chunk, adding them to Buffer (Buffer contains chunk#1).
Iteration 2: Reading next 1024 bytes (chunk#2) in Chunk, adding them to Buffer (Buffer contains chunk#1+chunk#2)
Iteration 3: Reading last 500 bytes (chunk#3) in Chunk. Now the condition "if (LineLength >= 0) then" is True. Adding chunk#3 to Buffer (now it contains chunk#1+chunk#2+chunk#3) and calling ProcessLine(Buffer);
Everything works as expected.
The lines
Stream.ReadBuffer(PRVAnsiChar(Chunk)^, ChunkSize);
and
Stream.ReadBuffer(Chunk[1], ChunkSize);
are equivalent.
Re: Out of memory in MakeDocumentFromStream
Hi,
I’m using version 22.3.1 with Delphi 12.1.
Perhaps the term "out of memory" is incorrect, and it’s more about a loss of data from the first chunk.
This happens when the chunk is not modified before or after the subsequent ReadBuffer.
I believe the bug is in Delphi (though I’m not sure if it affects all versions).
When using the PAnsiChar cast, the system doesn’t detect the change in the chunk string and doesn’t check the reference counter.
As a result, the Buffer variable is the same of the new data of chunck e lost the first read data.
Therefore, even though the two lines of code are equivalent for the compiler, something changes in the behavior.
with this you can repdocue the error
var
S: TRVAnsiString;
Stream: TStream;
begin
S := 'zama99o co7 mucb 6a0cb6da ( b66ocbto) , zama99o chakdk 0k 9uò 6bqqau7qk6mo a7 7kqozao , mUcb 6a09o7dk da 7o , zama99o 09akqb chk hb ba0oq7o da 0u99o6to k 6k7k6da b7d6b77o b zb6k u7 0o96bmmuoqo db cbmcbq7o ,m''uzzacao 9Omazab mocbmk uzz U6bb7a0tacb , k do66b77o zb6k u7b dazzadb , qaà 0b chk a mb6o6a 7o7 0o7o 0tbta zbtta , hb zbtto u7 cb0a7o , k qaà 0b chk 7o7 hb 7k00u7b a7tk7zao7k da a7azab6k , 7kmmk o9k6k 96a6btk chk 9ubbmachk , 6a7combtk db u7b co76k7zao7k , chk b6k6b c6kbto 96obmkma 9k6 2 b77a , k 0a 0tb 9b6mb7do dkmmb 0t6bdb dka duk 0b7tub6a da 6ab cb6ktto , k qma 09akqb chk am 96obmkmb da Cbmcbq7o è u7 96obmkmb 7oto , 0b77o qma a7t6kcca chk c''k6b7o co7 a 0uoa 6aca7a , dbqma b0zbmta chk 0tb zbck7do 0ub moqmak , k mua b6k6b co7co6dbto co7 m''a7qkq7k6k cb9o k co7 0ub moqmak mb 7kck00atà a7 u6qk7zb da zb6k 6ab Cb6ktto , 9k6chè m''hb77o mk00b bmm''a7tk67o da 6uk0tb co76k7zao7k 3 b77a zb k 7k00u7o c''hb zbtto 7ak7tk è u7b 0t6bdb comu7bmk k co7ta7ub7o bd b66a6b6k 0kq7bmbzao7k dba cattbda7a k ba0oq7b zb6mb , k m''a7qkq7k6k cb9o qma hb dktto da zb6qmakmb 6kdk6k 9k6chè 0k è bmm''a7tk67o dkmmb co76k7zao7k da zk66b7do , mkttk mbmk b7db6k b zb6k u7''o9k6b chk a7 tko6ab mb do66kbbk zb6k u7''bmt6b 9k60o7b ,mb co76k7zao7k 96k6kdk chk mua 6bdb b zb6k mb 0t6bdb dka 2 0b7tub6a k chk b0zbmta 6ukmmb 9b6tk ma k qma mkqqk m''b6t. 9 .....k7t6o am tkma7k da 6 mk0a dbmmb dbtb dkmmb 0otto0c6azao7k dkm 12.04.2023, k qma dack mo6o hb77o tutto co9k6to db u7b zadkau00ao7k k ma qma dack chk a7 cb0o da a7bdkm9ak7zb dkm 0oqqktto bttubto6k , zama99o co7ta7ub b mkqqk6qma mk 6oca dkmmb co76k7zao7k , da6k 0a dack bmt6k0ì chk am comu7k 96k6ab dazzadb 9ot6à 0o09k7dk6k m''k0kcuzao7k dka mb6o6a ..., 96o0kquk b mkqqk6k mb co76k7zao7k ,9oa zama99o 09akqb chk mua dbmmb 0ub 9b6tk 96a6btb 7o7 hb zbtto 7ak7tk , 9k6 mb 9b6tk 9ubbmacb hb dbto u7 a7azao mb6o6a ,hb zbtto u7 mkzzo 0k7tak6a7o , dazzo6mk db 6ukmmo chk k6b 96k6a0to dbm 96oqktto k 9oa hb dbto u7b umtambzao7k 9b6zabmk dka mb6o6a k o6b b7d6b77o b 6kdk6k db0 db0db0 bb 0db0d b0db0 b0dd b0db0d0b ';
// S :=
Stream := RVU_AnsiStringToAnsiStream(S);
Stream.Position := 0;
RichViewEdit1.MarkdownProperties.LineWidth := 0;
RichViewEdit1.Clear;
RichViewEdit1.LoadMarkdownFromStream(Stream,'',0);
Stream.Free;
RichViewEdit1.Format;
ShowMessage(IntToStr(Length(RichViewEdit1.GetItemText(0))));
I’m using version 22.3.1 with Delphi 12.1.
Perhaps the term "out of memory" is incorrect, and it’s more about a loss of data from the first chunk.
This happens when the chunk is not modified before or after the subsequent ReadBuffer.
I believe the bug is in Delphi (though I’m not sure if it affects all versions).
When using the PAnsiChar cast, the system doesn’t detect the change in the chunk string and doesn’t check the reference counter.
As a result, the Buffer variable is the same of the new data of chunck e lost the first read data.
Therefore, even though the two lines of code are equivalent for the compiler, something changes in the behavior.
with this you can repdocue the error
var
S: TRVAnsiString;
Stream: TStream;
begin
S := 'zama99o co7 mucb 6a0cb6da ( b66ocbto) , zama99o chakdk 0k 9uò 6bqqau7qk6mo a7 7kqozao , mUcb 6a09o7dk da 7o , zama99o 09akqb chk hb ba0oq7o da 0u99o6to k 6k7k6da b7d6b77o b zb6k u7 0o96bmmuoqo db cbmcbq7o ,m''uzzacao 9Omazab mocbmk uzz U6bb7a0tacb , k do66b77o zb6k u7b dazzadb , qaà 0b chk a mb6o6a 7o7 0o7o 0tbta zbtta , hb zbtto u7 cb0a7o , k qaà 0b chk 7o7 hb 7k00u7b a7tk7zao7k da a7azab6k , 7kmmk o9k6k 96a6btk chk 9ubbmachk , 6a7combtk db u7b co76k7zao7k , chk b6k6b c6kbto 96obmkma 9k6 2 b77a , k 0a 0tb 9b6mb7do dkmmb 0t6bdb dka duk 0b7tub6a da 6ab cb6ktto , k qma 09akqb chk am 96obmkmb da Cbmcbq7o è u7 96obmkmb 7oto , 0b77o qma a7t6kcca chk c''k6b7o co7 a 0uoa 6aca7a , dbqma b0zbmta chk 0tb zbck7do 0ub moqmak , k mua b6k6b co7co6dbto co7 m''a7qkq7k6k cb9o k co7 0ub moqmak mb 7kck00atà a7 u6qk7zb da zb6k 6ab Cb6ktto , 9k6chè m''hb77o mk00b bmm''a7tk67o da 6uk0tb co76k7zao7k 3 b77a zb k 7k00u7o c''hb zbtto 7ak7tk è u7b 0t6bdb comu7bmk k co7ta7ub7o bd b66a6b6k 0kq7bmbzao7k dba cattbda7a k ba0oq7b zb6mb , k m''a7qkq7k6k cb9o qma hb dktto da zb6qmakmb 6kdk6k 9k6chè 0k è bmm''a7tk67o dkmmb co76k7zao7k da zk66b7do , mkttk mbmk b7db6k b zb6k u7''o9k6b chk a7 tko6ab mb do66kbbk zb6k u7''bmt6b 9k60o7b ,mb co76k7zao7k 96k6kdk chk mua 6bdb b zb6k mb 0t6bdb dka 2 0b7tub6a k chk b0zbmta 6ukmmb 9b6tk ma k qma mkqqk m''b6t. 9 .....k7t6o am tkma7k da 6 mk0a dbmmb dbtb dkmmb 0otto0c6azao7k dkm 12.04.2023, k qma dack mo6o hb77o tutto co9k6to db u7b zadkau00ao7k k ma qma dack chk a7 cb0o da a7bdkm9ak7zb dkm 0oqqktto bttubto6k , zama99o co7ta7ub b mkqqk6qma mk 6oca dkmmb co76k7zao7k , da6k 0a dack bmt6k0ì chk am comu7k 96k6ab dazzadb 9ot6à 0o09k7dk6k m''k0kcuzao7k dka mb6o6a ..., 96o0kquk b mkqqk6k mb co76k7zao7k ,9oa zama99o 09akqb chk mua dbmmb 0ub 9b6tk 96a6btb 7o7 hb zbtto 7ak7tk , 9k6 mb 9b6tk 9ubbmacb hb dbto u7 a7azao mb6o6a ,hb zbtto u7 mkzzo 0k7tak6a7o , dazzo6mk db 6ukmmo chk k6b 96k6a0to dbm 96oqktto k 9oa hb dbto u7b umtambzao7k 9b6zabmk dka mb6o6a k o6b b7d6b77o b 6kdk6k db0 db0db0 bb 0db0d b0db0 b0dd b0db0d0b ';
// S :=
Stream := RVU_AnsiStringToAnsiStream(S);
Stream.Position := 0;
RichViewEdit1.MarkdownProperties.LineWidth := 0;
RichViewEdit1.Clear;
RichViewEdit1.LoadMarkdownFromStream(Stream,'',0);
Stream.Free;
RichViewEdit1.Format;
ShowMessage(IntToStr(Length(RichViewEdit1.GetItemText(0))));
-
- Site Admin
- Posts: 17679
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Re: Out of memory in MakeDocumentFromStream
I confirm the problem.
In this method, change
to
Delphi help file says:
In this method, change
Code: Select all
if Length(Chunk) <> ChunkSize then
SetLength(Chunk, ChunkSize);
Code: Select all
SetLength(Chunk, ChunkSize);
Following a call to SetLength, S is guaranteed to reference a unique string or array -- that is, a string or array with a reference count of one
-
- Site Admin
- Posts: 17679
- Joined: Sat Aug 27, 2005 10:28 am
- Contact:
Re: Out of memory in MakeDocumentFromStream
Fixed in TRichView v23.1