public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug c++/47940] New: can call a pure virtual from a constructor/destructor
@ 2011-03-01 10:10 mlg7 at yandex dot ru
  2011-03-01 10:14 ` [Bug c++/47940] " pinskia at gcc dot gnu.org
                   ` (7 more replies)
  0 siblings, 8 replies; 9+ messages in thread
From: mlg7 at yandex dot ru @ 2011-03-01 10:10 UTC (permalink / raw)
  To: gcc-bugs

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=47940

           Summary: can call a pure virtual from a constructor/destructor
           Product: gcc
           Version: 4.4.5
            Status: UNCONFIRMED
          Severity: enhancement
          Priority: P3
         Component: c++
        AssignedTo: unassigned@gcc.gnu.org
        ReportedBy: mlg7@yandex.ru


A colleague of mine have been examining a crash that
boils down to the following:
--hellobug.cpp--
class Base {
public:
    Base() { usefunc(); }
    virtual void func()=0;
    void usefunc() { func(); }
};

class Derived: public Base {
public:
    virtual void func() {}
};

int main() {
    Derived d;
    return 0;
}
--eof--

$ g++ -Wall -Wextra hellobug.cpp
{just nothing}
$ ./a.out 
pure virtual method called
terminate called without an active exception
Aborted

That is, "you asked for it, you got it" [1].
==
[1] from "Real Programmers Don't"
==

A variation:

==byebug.cpp==
#include <stdio.h>
class Base {
public:
    ~Base() { usefunc(); }
    virtual void func()=0;
    void usefunc() { func(); }
};

class Derived: public Base {
public:
    virtual void func() {}
};

int main() {
    Derived d;
    printf("life is good\n");
    return 0;
}
==eof==

$ g++ -Wall -Wextra byebug.cpp
{again nothing}
$ ./a.out 
life is good
pure virtual method called
terminate called without an active exception
Aborted


I have to note that in a big software project, 
the mistake is not so obvious.
But what can be done?


Functions that call pure virtual functions cannot
be called from constructors and destructors.
This may be discovered at compile-time, and the above
text makes a good error/warning message.



WHAT I GET: no warning
WHAT I WOULD EXPECT: a warning or an error message:
"Functions that call pure virtual functions cannot
be called from constructors and destructors"


$ gcc -v
Using built-in specs.
Target: x86_64-linux-gnu
Configured with: ../src/configure -v --with-pkgversion='Ubuntu/Linaro
4.4.4-14ubuntu5' --with-bugurl=file:///usr/share/doc/gcc-4.4/README.Bugs
--enable-languages=c,c++,fortran,objc,obj-c++ --prefix=/usr
--program-suffix=-4.4 --enable-shared --enable-multiarch
--enable-linker-build-id --with-system-zlib --libexecdir=/usr/lib
--without-included-gettext --enable-threads=posix
--with-gxx-include-dir=/usr/include/c++/4.4 --libdir=/usr/lib --enable-nls
--with-sysroot=/ --enable-clocale=gnu --enable-libstdcxx-debug --enable-objc-gc
--disable-werror --with-arch-32=i686 --with-tune=generic
--enable-checking=release --build=x86_64-linux-gnu --host=x86_64-linux-gnu
--target=x86_64-linux-gnu
Thread model: posix
gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)


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

end of thread, other threads:[~2024-06-11 14:32 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2011-03-01 10:10 [Bug c++/47940] New: can call a pure virtual from a constructor/destructor mlg7 at yandex dot ru
2011-03-01 10:14 ` [Bug c++/47940] " pinskia at gcc dot gnu.org
2011-03-01 11:34 ` mlg7 at yandex dot ru
2011-03-01 12:19 ` [Bug c++/47940] warn about calls to " redi at gcc dot gnu.org
2011-03-01 12:30 ` redi at gcc dot gnu.org
2011-03-01 12:32 ` redi at gcc dot gnu.org
2011-03-01 17:23 ` mlg7 at yandex dot ru
2024-06-11 14:31 ` pinskia at gcc dot gnu.org
2024-06-11 14:32 ` [Bug c++/47940] warn about calls to a pure virtual indirectly " pinskia at gcc dot gnu.org

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).