c++/6006: problem with pure virtual functions and destructord

jody@ifi.unizh.ch jody@ifi.unizh.ch
Tue Mar 19 11:46:00 GMT 2002


>Number:         6006
>Category:       c++
>Synopsis:       problem with pure virtual functions and destructord
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    unassigned
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Tue Mar 19 10:06:00 PST 2002
>Closed-Date:
>Last-Modified:
>Originator:     jody weissmann
>Release:        gcc version 2.95.2 19991024 (release)
>Organization:
>Environment:
sparc-sun-solaris2.8
>Description:
A pure virtual method f() of a class A CANNOT be called from within the destructor ~A of the class A.
However, it can be called from within a normal method g() of A.
But strangely enough,  g() can be called from within the destructor ~A of A.

The error message created when f9) is called from within ~A:
Undefined                       first referenced
 symbol                             in file
A::f(void)                          BugProg.o
ld: fatal: Symbol referencing errors. No output written to BugProg
collect2: ld returned 1 exit status
>How-To-Repeat:
The error appears when the program below is compiled:
g++ -o BugProg BugProg.cpp

Here comes the source of BugProg.cpp
//-------------------------------------
// BugProg.cpp
//
//-------------------------------------

//-------------------------------------
// declaration of A
//
class A {
public:
  A(void);
 ~A(void);
 
  virtual void f(void)=0;
  void g(void);
};


//-------------------------------------
// implementation of B
//
class B : public A {
public:
  B(void);
 
  virtual void f(void);
};


//-------------------------------------
// implementation of A
//
A::A(void) {
}

A::~A(void) {
  f();    // this doesn't work
  g();     // but this works
}

void A::g(void) {
  f();
}  


//-------------------------------------
// implementation of B
//
B::B(void) 
  : A() {
}

void B::f(void) {
}


//-------------------------------------
// main
//
void main(void) {
  B *pB = new B();
  delete pB;
}
>Fix:
Workaround: 
if a normal method g() of A calls the pure virtual method f() of A,
then g() can be called from within the destructor
>Release-Note:
>Audit-Trail:
>Unformatted:
----gnatsweb-attachment----
Content-Type: application/octet-stream; name="BugProg.ii"
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="BugProg.ii"

IyAxICJCdWdQcm9nLmNwcCIKIAogCiAKY2xhc3MgQSB7CnB1YmxpYzoKICBBKHZvaWQpOwogfkEo
dm9pZCk7CiAKICB2aXJ0dWFsIHZvaWQgZih2b2lkKT0wOwogIHZvaWQgZyh2b2lkKTsKfTsKCgog
CiAKIApjbGFzcyBCIDogcHVibGljIEEgewpwdWJsaWM6CiAgQih2b2lkKTsKIAogIHZpcnR1YWwg
dm9pZCBmKHZvaWQpOwp9OwoKCiAKIAogCkE6OkEodm9pZCkgewp9CgpBOjp+QSh2b2lkKSB7CiAg
ZigpOyAgICAgCiAgZygpOyAgICAgIAp9Cgp2b2lkIEE6Omcodm9pZCkgewogIGYoKTsKfSAgCgoK
IAogCiAKQjo6Qih2b2lkKSAKICA6IEEoKSB7Cn0KCnZvaWQgQjo6Zih2b2lkKSB7Cn0KCgogCiAK
IAp2b2lkIG1haW4odm9pZCkgewogIEIgKnBCID0gbmV3IEIoKTsKICBkZWxldGUgcEI7Cn0K



More information about the Gcc-bugs mailing list