Solved

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

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

Accepted Solution

by:
Sinisa Vuk earned 500 total points
Comment Utility
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
Comment Utility
ahh you are right Sinisa... thank you my friend!

Cheers
   Shawn
0

Featured Post

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

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…
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

772 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now