?
Solved

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

Posted on 2014-07-25
2
Medium Priority
?
496 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
[X]
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
2 Comments
 
LVL 27

Accepted Solution

by:
Sinisa Vuk earned 2000 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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

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…
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…
Have you created a query with information for a calendar? ... and then, abra-cadabra, the calendar is done?! I am going to show you how to make that happen. Visualize your data!  ... really see it To use the code to create a calendar from a q…
In this video, Percona Director of Solution Engineering Jon Tobin discusses the function and features of Percona Server for MongoDB. How Percona can help Percona can help you determine if Percona Server for MongoDB is the right solution for …
Suggested Courses
Course of the Month14 days, 18 hours left to enroll

771 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