Solved

"Invald Pointer Operation" on Setlength of dynamic array

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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

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…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
This Micro Tutorial demonstrates using Microsoft Excel pivot tables, how to reverse engineer competitors' marketing strategies through backlinks.
Two types of users will appreciate AOMEI Backupper Pro: 1 - Those with PCIe drives (and haven't found cloning software that works on them). 2 - Those who want a fast clone of their boot drive (no re-boots needed) and it can clone your drive wh…

773 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