Re: COMPILE!

From: tom_usenet (tom_usenet_at_hotmail.com)
Date: 08/05/04


Date: Thu, 05 Aug 2004 17:56:49 +0100

On Thu, 05 Aug 2004 15:17:26 GMT, JKop <NULL@NULL.NULL> wrote:

>g++ cnt.cpp -ansi -pedantic -o c.exe
>
>C:\WINDOWS\TEMP/ccgrbaaa.o(.text$_ZN15AutoDestructiveIiED2Ev+0x16):cnt.cpp:
>undefined reference to `AutoDestructive<int>::CleanUp()'
>
>
>Can some-one else please try compile it and see what happens.
>
>Anyone know what's wrong with the code?
>
>
>Here's the code:
>
>
>typedef int HKEY;
>const int ERROR_SUCCESS(5);
>
>void RegCloseKey(HKEY hkey) {}
>int RegOpenKeyEx(HKEY *hkey) { return ERROR_SUCCESS; }
>
>template<class T>
>class AutoDestructive
>{
>public:
>
> T t;
> bool to_be_auto_destructed;
>
> AutoDestructive() : t(), to_be_auto_destructed(false) {}
>
> virtual void CleanUp() = 0;
>
> ~AutoDestructive()
> {
> this->CleanUp();

There's your problem - calling a virtual function from a destructor
doesn't do what you think it does. Calling a pure virtual function
from a destructor causes undefined behaviour (and fortunately VC seems
to indicate this with a linker error, which is as nice as undefined
behaviour gets).

Basically, destruction goes for typical implementations:

derived class destructor called
derived members destructed in reverse order of declaration
*vtable pointer updated to point to base class vtable*
base class destructor called
base class members destructed in reverse order of declaration

The emphasised line is what causes the problem for you - calling a
virtual function from a constructor or destructor calls the function
in the base object currently being constructed or destructed, not the
one in the most derived class.

Tom



Relevant Pages

  • Re: Virtual destructors are unique virtuals right
    ... The destructor of a derived class does not call the ... >> of its base class, regardless of whether the base class has a virtual ... constructor for the BaseClass, then calls the constructor for the ... DerivedClass. ...
    (comp.lang.cpp)
  • Re: Odd behavior, vector member, MFC and consol app
    ... someone call the base class destructor which then ended up doing something bad. ... it should have no virtual functions at all. ... A good clue that a class is not intended for derivation is the absence ...
    (microsoft.public.vc.mfc)
  • Re: Does destructor of derived class remove virtual table?
    ... >I'm calling a virtual function in the destructor of the base class, ... You can get the FAQ at: ...
    (comp.lang.cpp)
  • Re: Cant compile this code *****SOLVED****
    ... your real program might contain hundreds of code sequences ... that the base class implements a 'virtual' ... destructor, so that when de-allocating objects created via ...
    (alt.comp.lang.learn.c-cpp)
  • Re: inhibit compiler warning C4624 for a class hierarchy
    ... Any class must have a destructor before you can ... create (or, to be exact, destroy) an instance of it. ... implicitly-declared destructor, ... a base class with an inaccessible destructor. ...
    (microsoft.public.vc.language)