public inbox for gcc-prs@sourceware.org
help / color / mirror / Atom feed
* c++/6006: problem with pure virtual functions and destructord
@ 2002-03-19 10:06 jody
  0 siblings, 0 replies; 4+ messages in thread
From: jody @ 2002-03-19 10:06 UTC (permalink / raw)
  To: gcc-gnats


>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


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: c++/6006: problem with pure virtual functions and destructord
@ 2002-05-15  3:16 Nathan Sidwell
  0 siblings, 0 replies; 4+ messages in thread
From: Nathan Sidwell @ 2002-05-15  3:16 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/6006; it has been noted by GNATS.

From: Nathan Sidwell <nathan@acm.org>
To: jody@ifi.unizh.ch
Cc: lerdsuwa@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org, 
    nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Subject: Re: c++/6006: problem with pure virtual functions and destructord
Date: Wed, 15 May 2002 11:06:07 +0100

 jody@ifi.unizh.ch wrote:
 > 
 > Hi
 > Is there a particular reason why a a virtual function called
 > in a destructor is the function of this class and not the derived class?
 because the standard says so.
 
 > (There are implementations of C++ where the virtual
 > function of the derived class is called)
 those implementations are buggy.
 
 > In my example, however, the function g() calls the pure virtual
 > function f(). But g() can be called from the destructor of A,
 > without causing a problem.
 neither call 'works'. Drat, someone's borrowed my c++ std, so this is
 from memory. in the dtor of A, the B object is not complete (B's
 dtor has been executed, modulo the bits pertaining to its bases).
 We shouldn't call (non-static) member functions of B, because
 there's no constructed B object for their this pointer to point to.
 The std says, that in this case, virtual functions dispatch to the
 function seen in the definition of the base being ctor'd or dtor'd
 (i.e. what the final overrider would be if that base was the complete
 object).
 
 IIRC, calling a pure virtual function is undefined, so anything can
 happen. g++ 3.0 reports
 6006.cc:37: abstract virtual `virtual void A::f()' called from destructor
 because it can prove that that resolved to a pure virtual. It cannot
 do so for the one embedded in g, but if you executed such a program
 it would fail.
 
 nathan
 
 -- 
 Dr Nathan Sidwell :: Computer Science Department :: Bristol University
            The voices in my head told me to say this
 nathan@acm.org  http://www.cs.bris.ac.uk/~nathan/  nathan@cs.bris.ac.uk


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: c++/6006: problem with pure virtual functions and destructord
@ 2002-05-15  1:46 jody
  0 siblings, 0 replies; 4+ messages in thread
From: jody @ 2002-05-15  1:46 UTC (permalink / raw)
  To: nobody; +Cc: gcc-prs

The following reply was made to PR c++/6006; it has been noted by GNATS.

From: jody@ifi.unizh.ch
To: lerdsuwa@gcc.gnu.org, gcc-bugs@gcc.gnu.org, gcc-prs@gcc.gnu.org,
       jody@ifi.unizh.ch, nobody@gcc.gnu.org, gcc-gnats@gcc.gnu.org
Cc:  
Subject: Re: c++/6006: problem with pure virtual functions and destructord
Date: Wed, 15 May 2002 10:41:57 +0200 (CEST)

 Hi
 Is there a particular reason why a a virtual function called
 in a destructor is the function of this class and not the derived class?
 (There are implementations of C++ where the virtual
 function of the derived class is called)
 
 Anyway, the gnu implementation of c++ does not match (section 12.7 para3)
 >     [section 12.7 para 3]
 >     ... When a virtual function is called directly or indirectly
 >     from a constructor (including from the mem-initializer for a
 >     data member) or from a destructor, and the object to which
 >     the call applies is the object under construction or destruction,
 >     the function called is the one defined in the constructor or
 >     destructor's own class or in one of its base, but not a function
 >     overriding it in a class derived from the constructor or
 >     destructor's class, or overriding it in one of the other
 >     base classes of the most derived object (1.8).
 > 
 This paragraph states that if the virtual function is called
 directly OR INDIRECTLY from a constructor or destructor, then
 the virtual function of the class itself is called.
 
 In my example, however, the function g() calls the pure virtual
 function f(). But g() can be called from the destructor of A,
 without causing a problem.
 
 Thanks 
   Jody Weissmann
   Computer Science Department
   University of Zurich
   Winterthurerstr 190
   8057 Zurich
   Switzerland
   Phone: +41(0)1 635 43 17
   EMail: jody@ifi.unizh.ch


^ permalink raw reply	[flat|nested] 4+ messages in thread

* Re: c++/6006: problem with pure virtual functions and destructord
@ 2002-05-14  8:46 lerdsuwa
  0 siblings, 0 replies; 4+ messages in thread
From: lerdsuwa @ 2002-05-14  8:46 UTC (permalink / raw)
  To: gcc-bugs, gcc-prs, jody, nobody

Synopsis: problem with pure virtual functions and destructord

State-Changed-From-To: open->closed
State-Changed-By: lerdsuwa
State-Changed-When: Tue May 14 08:46:22 2002
State-Changed-Why:
    Not a bug.  The virtual function is treated as non-virtual
    inside destructors.  So ~A() can only call A::f() which is
    pure virtual.  Here is the relevant part of the standard
    [section 12.7 para 3]
    ... When a virtual function is called directly or indirectly
    from a constructor (including from the mem-initializer for a
    data member) or from a destructor, and the object to which
    the call applies is the object under construction or destruction,
    the function called is the one defined in the constructor or
    destructor's own class or in one of its base, but not a function
    overriding it in a class derived from the constructor or
    destructor's class, or overriding it in one of the other
    base classes of the most derived object (1.8).

http://gcc.gnu.org/cgi-bin/gnatsweb.pl?cmd=view%20audit-trail&database=gcc&pr=6006


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2002-05-15 10:16 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2002-03-19 10:06 c++/6006: problem with pure virtual functions and destructord jody
2002-05-14  8:46 lerdsuwa
2002-05-15  1:46 jody
2002-05-15  3:16 Nathan Sidwell

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).