Re: Test if memory pointer is valid?
- From: "Paul E. Schoen" <pstech@xxxxxxxxx>
- Date: Fri, 27 Jan 2006 01:57:03 -0500
"Richard A. DeVenezia" <rdevenezia@xxxxxxxxxxxx> wrote in message
news:43tfb0F1npa5qU1@xxxxxxxxxxxxxxxxx
> Is there a way or technique for knowing if a pointer points to memory that
> has been freed ?
>
> 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);
>
> end;
>
> --
> Richard A. DeVenezia
>
>
I am not totally comfortable with pointers, especially as they are handled
in Delphi as compared to C, but at the basic level, a pointer is just a
memory address that contains the memory address of some data, which could be
anything from a single byte to an array, structure, or object, which may in
turn contain other pointers. If the pointer is nil, or all zeroes, it does
not point to anything. When it is initialized, it should contain the address
of memory which has been allocated for the size of the data type it
represents. Thus, if it is set to nil when it is freed, it can be checked
for validity.
My problem is that I don't know why a second variable should be set to point
to the same block of memory. I can possibly understand setting a pointer to
the original pointer, and perhaps interpreting the memory space in a
different way, using a typecast. This may be similar to the "C" concept of a
Union, in which several variable names all point to the same memory, but are
interpreted by the compiler in different ways. However, I think each
variable is compiled as the same address, so if any one is Freed and made
nil, a reference to any one is also nil.
I can't think of any reason to have two separate pointers to the same
address, but it must be common, as I have been informed previously about
multiple references. I can see setting a new pointer initially to the
address of the beginning of an array, for example, and then incrementing the
pointer to address subsequent elements, but that would usually be done
inside a function, where the new pointer would go out of scope at the end of
the function and be reinitialized if it is called again. I would like to see
a specific example where it is necessary to have multiple references such as
this where it would be necessary to check for it having been freed.
Possibly, I can see where two objects on a form, perhaps, might reference
fields in each other, but unless the object itself had been freed, the
reference should be valid, and you can always check to see if the object is
not nil.
If there is such a reason, would it be possible to assign a pointer to the
original pointer, and then use dereferencing to determine if the original
pointer was nil and therefore free, or otherwise safe to use?
Thanks for any insight, comments, explanations, and examples.
Paul E. Schoen
.
- Follow-Ups:
- Re: Test if memory pointer is valid?
- From: Bruce Roberts
- Re: Test if memory pointer is valid?
- From: Maarten Wiltink
- Re: Test if memory pointer is valid?
- From: Rob Kennedy
- Re: Test if memory pointer is valid?
- References:
- Test if memory pointer is valid?
- From: Richard A. DeVenezia
- Test if memory pointer is valid?
- Prev by Date: Re: Event handlers...
- Next by Date: Re: Test if memory pointer is valid?
- Previous by thread: Re: Test if memory pointer is valid?
- Next by thread: Re: Test if memory pointer is valid?
- Index(es):
Relevant Pages
|