Re:Invalid pointer operation.
"Steven Quail" <squai...@yahoo.com> skrev i melding
news:3aabc57a.0108300859.2ca7e09b@posting.google.com...
Quote
> I have the following procedure:
> Procedure (var Str : PChar)
> and inside the procedure I have
> Inc(Str).
> (I wanted to increment the pointer and
> pass it back to the calling parameter).
> In the calling program when I do a
> FreeMem(StrPass) where I defined
> StrPass as GetMem(StrPass, 100), I get
> the runtime error Invalid pointer operation.
> (Strpass is defined as PChar).
> In the help for FreeMem, it says that I would get an error
> if StrPass is not pointing to heap.
> Why do I get this error?
> Is this because I am trying to free the memory
> from a point that it was not assigned (i.e. I
> increased the pointer by 1).
> Also, is Inc(StrPass) the best way to scroll
> through a region of memory?
Yeah, you can't do FreeMem on a pointer incremented by one or more bytes. The
reason is:
When a GetMem is done, the pointer points to the 3rd of 4th byte of the
allocated memory block, not at the beginning. The preceeding bytes hold the
allocated memory size, so trying to free it would{*word*222}it all up. In
addition, the memory block is referenced by the Memory Manager, I believe the
exception is raised as it can't find the pointer value in it's list.
To avoid such problems, you need of course keep a reference to the original
start of block.
--
Bjoerge Saether
Consultant / Developer
http://www.itte.no
Asker, Norway
bjorge@takethisaway_itte.no (remve the obvious)