Re: Test if memory pointer is valid?



Richard A. DeVenezia wrote:
Is there a way or technique for knowing if a pointer points to memory that
has been freed ?

Yes. When you free it, remember that fact so you don't use the variable anymore.


The example is a little silly, in reality, P & Q might fields in instances
of disparate classes

P: Pointer;
Q: Pointer;
BuffSize: Integer;

BuffSize := 32000;
GetMem(P, BuffSize);
F.Read(P^,BuffSize);
...
Q := P;
...
if ...
    FreeMem(P,BuffSize)
...

if [ Q points to memory that has not been freed ]  // what technique, if
any, can test this?
  FreeMem (Q,BuffSize);

Keep track of who owns the memory. You have two references to it, but only one of them "owns" it. Only allow the owner to free it. If assigning P to Q also bestows ownership, then don't free the memory through P. Only free it through Q.


The other way is to let everyone be an owner, and when the memory stops being owned by anyone, free it. This involved counting the references to the memory. When you assign P to Q, the reference count needs to go up.

Given an arbitrary address, it is not possible to know whether you're allowed to call FreeMem on that address. Consider this:

GetMem(P, 20);
Q := P;
FreeMem(P);
GetMem(R, 20);

After those four lines run, it's quite likely that Q = R, so if there were such a function as IsValidPointer, then IsValidPointer(Q) would return True even though Q doesn't refer to the same thing you initialized it to. Freeing the memory via Q would succeed, but it would invalidate R's value.

The solution to all this? Write you program in such a way that you never need to make these sorts of checks in the first place. Keep strict tabs on who owns the memory.

--
Rob
.



Relevant Pages

  • Re: [patch 1/6] mmu_notifier: Core code
    ... external references to pages managed by the Linux kernel. ... access memory managed by the Linux kernel. ... The MMU notifier will notify the device driver that subscribes to such ...
    (Linux-Kernel)
  • Re: [PHP] Odd PHP memory issue
    ... all references to the result variable are unset when I ... to see if the memory is getting chewed up in a straight line or if it ... Scope seems like it should be simple, ... posting this question was to find out more about how PHP internals work, ...
    (php.general)
  • Re: Writing huge Sets() to disk
    ... that this code takes a lot of extra memory. ... > I believe it's the references problem, ... It's a bit unfortunate that all those instance variables are global to ... it merely aggregates it for use in storing new Python objects. ...
    (comp.lang.python)
  • Re: How to track memory usage?
    ... the IE memory leak issue is real, ... And JScript in IE allows the garbage collector to be explicitly ... but then removes all references to that object ... "Break Circular References" - examines all DIVs and removes ...
    (comp.lang.javascript)
  • Re: [PHP] Odd PHP memory issue
    ... to see if the memory is getting chewed up in a straight line or if it ... After unsetting ALL globals within the global context (there are no ... references to globals (all non-global variables have gone out of scope ... PHP "scope" is not as clean-cut as C. ...
    (php.general)