Re: Test if memory pointer is valid?
- From: Rob Kennedy <me3@xxxxxxxxxxx>
- Date: Fri, 27 Jan 2006 01:18:08 -0600
Paul E. Schoen wrote:
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.
Suppose you have a control on your form, an edit box, and when its text changes, it is capable of notifying any other objects that have expressed interest in that change. (This is known as the "observer" pattern.) The edit box keeps a list of interested objects, and when it changes, it sends a message to each object in the list informing it of the change.
Clearly, the edit box has a reference to each of those objects. Now suppose you have two edit boxes, and you have some object that wants to be notified when either one of those edit boxes changes. It will register itself with both edit boxes. There are now two references to that same object.
Here's another example. When you put a control on a form, the IDE declares a field for you in the form class to refer to that control at run time. Each field needs to have a different name. VB has a convenient feature that if several controls on a form all have the same name, then they are automatically members of an array instead of standalone fields. If you want that in Delphi, you need to generate the array yourself. To have an array of all the TLabel controls on your form, you'd declare an an array and then assign its elements later. The array would contain copies of all the TLabel references already in the form as individual fields. Now suppose you also want an array of all the controls on the upper half of your form. That includes some TLabels and some TEdits. The TLabels will of course already be in the TLabel array, but now they will also be in the upper-control array. The TLabel control doesn't know anything about either of the arrays since TLabel was written and compiled before those arrays existed, so when you free the TLabel, the control cannot automatically remove itself from the arrays. So when you free the TLabel, you're stuck with two stale references in your array.
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?
Dereference the second pointer to find out whether it's safe to dereference the first one? Sure, you could do that. But how will you know when it's safe to dereference the second pointer? And where would you keep the second pointer, anyway?
-- Rob .
- Follow-Ups:
- Re: Test if memory pointer is valid?
- From: Paul E. Schoen
- Re: Test if memory pointer is valid?
- References:
- Test if memory pointer is valid?
- From: Richard A. DeVenezia
- Re: Test if memory pointer is valid?
- From: Paul E. Schoen
- Test if memory pointer is valid?
- Prev by Date: Re: Test if memory pointer is valid?
- 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
|