Solved

"Invald Pointer Operation" on Setlength of dynamic array

Posted on 2014-11-11
2
259 Views
Last Modified: 2014-11-12
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
0
Comment
Question by:shawn857
2 Comments
 
LVL 19

Accepted Solution

by:
MerijnB earned 500 total points
ID: 40436783
I guestimate the problem lies in the StrCopy, where you cast the dynamic array to PChar.
Change it to a CopyMemory(), the exact code depends on whether these are ansi or unicode chars and strings (which Delphi version are you using?)

This will probably work:

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 + #13#10;
    hlength := length(inrec);

    SetLength(HOutBuffer2, hlength);
    CopyMemory(@HOutBuffer2[0], @inrec[1], hlength*sizeof(char));

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

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

Open in new window

0
 

Author Closing Comment

by:shawn857
ID: 40438740
Thanks Merijn, that did the trick!

Cheers
    Shawn
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Creating an auto free TStringList The TStringList is a basic and frequently used object in Delphi. On many occasions, you may want to create a temporary list, process some items in the list and be done with the list. In such cases, you have to…

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question