Topic: explicit destructor call through null pointer


Author: adk@Warren.MENTORG.COM (Ajay Kamdar)
Date: 25 Mar 1993 15:11:50 -0500
Raw View
In article <1993Mar24.185020.6132@microsoft.com> jimad@microsoft.com (Jim Adcock) writes:
>
>Really?  What code does cfront generate in this situation if the
>destructor is declared virtual?  Won't you still possibly get an
>attempted virtual dispatch through a null pointer?
>

You are correct. My observation that explicitly calling a destructor
via a null pointer happens to work with cfront's implementation is
true only for classes without a virtual destructor.

In the presence of a virtual destructor, cfront still generates code
which results in a call to a function which does both destruction and
frees memory depending upon its arguments. However, the way it does
virtual dispatch is different for the delete operator and for an explicit
call to the destructor. For the delete operator, it checks for the pointer
being zero before doing the dispatch; for the explicit call to the
destructor, it does the virtual dispatch with out checking for a zero
pointer. This is fine since the ARM guarantees only the deletion of a
null pointer to be harmless.

This further reinforces the point that the result of explicitly calling
the destructor via a null pointer is undefined.

- Ajay

--
I speak for no one but myself.

Ajay Kamdar                               Email : ajay_kamdar@mentorg.com
Mentor Graphics, IC Group (Warren, NJ)    Phone : (908) 604-0842