Link to home
Start Free TrialLog in
Avatar of shawn857
shawn857

asked on

"Invald Pointer Operation" on Setlength of dynamic array

Hi guys, I have a very simple program (I'm trying to do a little speed comparison between StrCopy, StrMove, and Move), and I keep getting an Invalid Pointer Operation on the 2nd time through my "while" loop on this line:

SetLength(HOutBuffer2, hlength);

Here's my code:

var
  Form1: TForm1;
  infiletext : TGpTextFile;
  outfilestream : TGpHugeFileStream;
  inrec : string;
  bytes_Wrote, hlength : integer;
  HOutbuffer2 : array of Char;

implementation

{$R *.dfm}


procedure TForm1.Button1Click(Sender: TObject);
begin
  infiletext:=TGpTextFile.CreateEx('bigtest.txt', FILE_ATTRIBUTE_NORMAL, GENERIC_READ);
  infiletext.Reset();
  outfilestream:=TGpHugeFileStream.Create('outstream.txt', accWrite);


  while not infiletext.EOF do
  begin
    inrec:=infiletext.Readln;
    hlength := length(inrec)+2;

    SetLength(HOutBuffer2, hlength);
    StrCopy(PChar(HOutBuffer2), PChar(inrec+#13#10));

    bytes_Wrote:=OutFileStream.Write(HoutBuffer2[0], hlength);
  end; // WHILE

  infiletext.Close;
  infiletext.Free;
  OutFileStream.Free;
end;

Open in new window


It does the 1st iteration of the loop fine, then crashes 2nd time through on "SetLength(HOutBuffer2, hlength);"

Thanks!
   Shawn
ASKER CERTIFIED SOLUTION
Avatar of MerijnB
MerijnB
Flag of Netherlands image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of shawn857
shawn857

ASKER

Thanks Merijn, that did the trick!

Cheers
    Shawn