Solved

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

Posted on 2014-07-25
2
452 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

Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

Question has a verified solution.

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

Suggested Solutions

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
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…
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…

808 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