Article

From:
To:
Dalija Prasnikar
Subject:
Re: Out of memory Exception when using large StringStream.DataString inD2009
Newsgroup:
embarcadero.public.delphi.rtl

Re: Out of memory Exception when using large StringStream.DataString inD2009

<Dalija Prasnikar> wrote in message news:✉forums.embarcadero.com...

> Actually, it would be the best if you skip using any kind of
> string stream and load string directly.

There are three problems with that code:

1) you are using RawByteString, but you are not specifying any codepage for it.
2) you need to use TStream.ReadBuffer() instead of TStream.Read() to ensure the entire string is read.
3) if Size is -1, but the TStream does not have anything left to read, you allocate a 0-length String (thus the String is nil) but then dereference it, which will crash.
Try this instead:
{code:delphi} function RawStringLoadFromStream(Stream: TStream; Size: Integer = -1): RawByteString; begin   if Size = -1 then Size := Stream.Size - Stream.Position;   if Size <= 0 then Result := ''   else   begin     SetLength(Result, Size);     Stream.ReadBuffer(Result[1], Size);     SetCodePage(Result, SomeCodePageHere, False);   end; end; {code}
-- Remy Lebeau (TeamB)
FYI: Phrase searches are enclosed in either single or double quotes
 
 
Originally created by
Tamarack Associates
Thu, 21 Nov 2024 20:31:40 UTC
Copyright © 2009-2024
HREF Tools Corp.