Solved

"Freemem" statement now gives "Invalid Pointer Operation". Used to work OK.

Posted on 2014-07-25
2
428 Views
Last Modified: 2014-07-28
Hello Experts... I have a routine which would read a "chunk" of bytes from a file using a stream, do a bit of processing, then write that chunk to an output stream.. and repeat this till nothing left to read. Before the read I have these statements:

    Chunksize := 2048000;
    GetMem(CopyBuffer, ChunkSize); // CopyBuffer is defined as a PChar

And I do the read as such:

ibytesOK:=InfileStream.Read(CopyBuffer^, ChunkSize);

At the end when cleaning things up and closing files, I have this statement:

FreeMem(CopyBuffer, Chunksize);


This all used to work fine, until I just added some code that steps through the CopyBuffer a bit, looking for a #13 character:

while CopyBuffer^ <> #13 do  
       Inc(CopyBuffer);

Now when I run it, it crashes on the FreeMem(CopyBuffer, Chunksize); statement giving "Invalid Pointer Operation". Any idea how to remedy this, anyone?

Thanks!
    Shawn
0
Comment
Question by:shawn857
2 Comments
 
LVL 26

Accepted Solution

by:
Sinisa Vuk earned 500 total points
ID: 40222853
CopyBuffer is a pointer variable - point to some memory block. You must never use it in such operation which can "force" to "point" to somewhere else. This Inc function is one of them. Rather use temporary pointer:

ptr: PChar;
len: Integer;
begin
...
ptr := CopyBuffer; //copy point location
len := 0;
while (ptr^ <> #13) and (len < ChunkSize) do   //chk length too
begin
     Inc(ptr);
     Inc(len);
end;

//use ptr after that....
...

//free mem
ptr := nil;
FreeMem(CopyBuffer);
end;

Open in new window


...always check if pointer location is nor run over....(ChunkSize)
0
 

Author Closing Comment

by:shawn857
ID: 40225462
ahh you are right Sinisa... thank you my friend!

Cheers
   Shawn
0

Featured Post

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
In a recent question (https://www.experts-exchange.com/questions/28997919/Pagination-in-Adobe-Acrobat.html) here at Experts Exchange, a member asked how to add page numbers to a PDF file using Adobe Acrobat XI Pro. This short video Micro Tutorial sh…
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

803 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