Path: utzoo!utgpu!water!watmath!clyde!cbosgd!ihnp4!alberta!calgary!lomow From: lomow@calgary.UUCP (Greg Lomow) Newsgroups: comp.lang.c++ Subject: Virtual Destructors Message-ID: <1297@vaxb.calgary.UUCP> Date: 19 Jan 88 21:36:33 GMT Organization: U. of Calgary, Calgary, Ab. Lines: 54 /* A friend of mine (Darrin West) posted a message asking about the use of virtual destructors. The answer was that the use of virtual destructors is legal. Having used them, I can atest to the fact that they are very useful. However, if you call delete AA when AA is NULL and the type of AA is "pointer to class a" where a's destructor is declared as virtual, a segmentation fault occurs. The C++ manual says that delete can be applied to any pointer, NULL or non-NULL. The example program shown below demonstrates this problem. If both pieces of code that are commented out are included in the program, a segmentation fault occurs. We conjecture that delete is trying to access the destructor for AA like C++ would access any other virtual routine; i.e., by accessing a table with pointers to virtual functions, however if AA is NULL then no such table exists. Is this a bug or are we using virtual destructors wrong? We are using version 1.2 of C++ on a Vax 11/780 running Unix 4.3. */ /* error if virtual dtor and delete AA ok if non-virtual dtor and delete AA ok if virtual dtor and delete AA commented out */ #include #define NULL 0 class a { public: a(); /* virtual*/ ~a(); }; a::a(){} a::~a(){ cout << "deleted\n";} main() { a *A = new a(); delete A; a *AA = NULL; // delete AA; } -- Greg Lomow lomow@cpsc.calgary.cdn or ....![ubc-vision,ihnp4]!alberta!calgary!lomow