Validity check before downcasting / exception handling?
Maurice.Lombardi at ujf-grenoble.fr
Wed Mar 21 19:53:16 CET 2001
Prof. A Olowofoyeku (The African Chief) wrote:
> On 21 Mar 2001, at 18:20, Maurice Lombardi wrote:
>> Prof. A Olowofoyeku (The African Chief) wrote:
>>> On 21 Mar 2001, at 16:15, Maurice Lombardi wrote:
>>>>> Yes, it does - but I am not sure which destructor you would be calling
>>>>> after such an assignment. Remember that instances of classes are
>>>>> pointers to the objects - so in the assignment, what you are moving
>>>>> around are pointers, and there may be gotchas there.
>>>> But as far as I remember delphi knows the actual type of an object at
>>>> run time from the address of the VMT (there is one VMT for each defined
>>>> object type) which he recovers at a fixed displacement from the address
>>>> given by the object pointer.
>>> Well, the example code causes an exception at the point where you call
>>> "h1.free" (if you do the assignment "h1 := h2"). This happens both under
>>> Delphi and Virtual Pascal (which is Delphi-compatible). So there is
>>> obviously an issue there.
>> yes but that's because in this code you do two free's. After the
>> assignment h1 and h2 point to the same location. You then can do only one
>> free (h1.free or h2.free), in any order. The exception arises only on the
>> second free because the second pointer points then to nowhere.
> You are right of course. But that is still one of the "gotchas" that one
> needs to bear in mind, although it is a different one from the one that I
> had in mind at the time. And furthermore you cannot then be sure that
> there are no memory leaks - you have constructed two objects and
> allocated memory and resources for both of them, and you are only freeing
> one of them. That sounds like a memory leak to me, unless I am missing
Right, but it is a programming error the way the program has been written. The correct way would be to free h1 before
assigning it a new value, or to keep the corresponding address in an other pointer, otherwise the location it was
pointing to before the affectation is lost.
But this is of no consequence on the original issue. There is a well defined way to free correctly at run time the
content of an object.
Hope this helps
Laboratoire de Spectrometrie Physique,
Universite Joseph Fourier de Grenoble, BP87
38402 Saint Martin d'Heres Cedex FRANCE
Tel: 33 (0)4 76 51 47 51
Fax: 33 (0)4 76 51 45 44
mailto:Maurice.Lombardi at ujf-grenoble.fr
More information about the Gpc