Re: Virtual Destructor - Implication & Specification
* Le Chaud Lapin:
Let us re-examine what happens when a delete expression within an EXE
is applied to a pointer to an object that was manufactured inside a
DLL:
1. The EXE loads the DLL into its process address space.
2. An object gets "manufactured" in DLL.
3. A DLL function returns pointer to this object to a function in the
EXE.
4. EXE invokes operator delete against the pointer.
5. The class declaration for object indicates that class has virtual
desctructor.
6. EXE delete operation involves finding 'scalar deleting destructor'
via v-table, invokes it.
7. 'scalar deleting destructor' *IS A WHOLY-FORMED FUNCTION INSIDE
THE DLL*.
8. 'scalar deleting destructor' invokes tilde (~) function against
space.
9. 'scalar deleting destructor' calls equivalent of C free() function
against space.
Point 9 may well be what's going in with a particular C++
implementation, because a C++ implementation is free to implement
destructors any way it pleases as long as they conform to the standard
(e.g., the implementation can pass a boolean argument telling the
destructor whether to deallocate or not).
However, it's not guaranteed by the standard.
From a standard's point of view deallocation and destructor invocation
are unrelated. Conceptually a delete expression first invokes the
object's destructor, and then deallocates. This is perhaps most obvious
when you consider a dynamically allocated array of objects, where each
object's destructor is invoked, and then the array's storage is deallocated.
From a standard's point of view virtual destructor or not is simply not
relevant to deallocation, except that a non-virtual destructor causes
Undefined Behavior when destroying an object polymorphically.
The connection between virtual destructor and deallocation with a
particular compiler on a particular platform when using a particular
non-standard extension ("dllexport" of a class) is simply that and no
more: a non-standard way to let the compiler do automatically what you
could do manually, namely -- at least at the conceptual level --
implementing a class-specific allocation function (operator new) and
deallocation function (operator delete).
--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]