Solved

"Invald Pointer Operation" on Setlength of dynamic array

Posted on 2014-11-11
2
243 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

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

759 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

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now