Re: Test if memory pointer is valid?




"Rob Kennedy" <me3@xxxxxxxxxxx> wrote in message
news:43tvpcF1ooqt8U1@xxxxxxxxxxxxxxxxx
> Paul E. Schoen wrote:
> > I can't think of any reason to have two separate pointers to the same
> > address, ...
>
> 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.

Maybe I have never had a need for such a specialized type of edit box. I
would have a single event handler that would be called upon the OnChange
event of both edit boxes, check for the sender, and then inform the
interested object of the change and its origin. Each message box would
reference that procedure, but not an object that could be freed and cause an
error. The procedure would be part of the parent or owner form, which would
not be freed by any of its children or "slaves".
>
> 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.

I am doing just that, as discussed in a previous thread on array of
controls, but I have no need for two separate arrays. I would probably
hard-code references to the portions of the arrays I wanted to treat
separately. If I really wanted to treat them differently, I might create
record structures with pointers to the appropriate controls. Also, I would
not free the newly created arrays until the form itself was freed, which
would take care of it automatically.
>
> > 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?

Any secondary pointers would be most likely created in dynamically created
objects within the same form as the target of the original object, and would
be freed when that object was freed. Unless the second pointer is nil, it
should be safe to dereference it and find the address of the primary
pointer. If that is not nil, then it should be OK to use the secondary
pointer for any reference to the primary object. Otherwise, if the primary
pointer is always freed and nilled, any secondary references would know not
to attempt any access of the freed object.

Paul


.



Relevant Pages

  • Re: Test if memory pointer is valid?
    ... 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. ... copies of all the TLabel references already in the form as individual ...
    (comp.lang.pascal.delphi.misc)
  • Re: char **argv & char *argv[]
    ... "pointer to pointer to char". ... >> pointer)) pointing to the first element of an array. ... so we have to start adding more context. ... type "pointer to char", rather than "array MISSING_SIZE of char". ...
    (comp.lang.c)
  • Re: why cannot assign to function call
    ... hypothetical C-like languages, ... sizeof business would still indicate that a pointer was being passed. ... talk about variables of an array type. ... the earlier version of the standard didn't have numbered ...
    (comp.lang.python)
  • Re: multi dimensional arrays as one dimension array
    ... please - where does the standard say that such a conversion ... Pointer conversion yields a pointer to the same object as ... exist only where there are array declarations. ...
    (comp.lang.c)
  • Re: Evaluating unary *
    ... 'arr' exists, ... value can be used with the same syntax as would be used to access a 2D array of the kind you're referring to, but that 2D array is just a different way of looking as the same object that was already created by the definition of 'arr'. ... to me, it makes sense to return a pointer to the first value of an array, but to return the address of the pointer to the first value of an array, is not directly possible as such. ... lea eax, ...
    (comp.std.c)