Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

"Invald Pointer Operation" on Setlength of dynamic array

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Email security requires an ever evolving service that stays up to date with counter-evolving threats. The Email Laundry perform Research and Development to ensure their email security service evolves faster than cyber criminals. We apply our Threat…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

860 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