0

Referring to this question, How to resolve "pure virtual method called" is there a compiler option, that raises error when a virtual method is called from constructor/destructor? When there is a pure virtual method, the program will crash at runtime with segmentation fault.

This is proven many times to be a bad design:

When you call virtual methods from within your constructors/destructors it's not the overriden versions of them that are called.

EDIT:

I am asking specifically for MSVC, but it would be useful if there is an option for each compiler (GCC, CLANG, etc).

Dr. Gut
  • 2,053
  • 7
  • 26
Alexandru Irimiea
  • 2,513
  • 4
  • 26
  • 46

2 Answers2

3

is there a compiler option that raises error when a virtual method is called from constructor/destructor? When there is a pure virtual method, the program will crash at runtime with segmentation fault.

but it would be useful if there is an option for each compiler (GCC, CLANG, etc).

For GCC and Clang, the option is -Werror. This will cause all warnings to raise an error. If you directly call a pure virtual function in a constructor or destructor, both compilers warn by default.

There is of course no warning / error if a non-pure virtual function is called. It has well defined behaviour. Also, a compiler can not in general detect indirect calls to pure virtual functions.

I don't know of MSVC.

This is proven many times to be a bad design:

If you refer to calling a pure virtual function virtually in ctor./dtor., then bad design is an understatement. The behaviour is undefined. If you're unlucky, the program might not even crash.

Community
  • 1
  • 1
eerorika
  • 232,697
  • 12
  • 197
  • 326
  • @Cheersandhth.-Alf about your edit: Is it even possible to call a function virtually in c-/d-tor? As I understand, the call is always non-virtual, and that is why it is wrong since pure virtual functions might not even have a definition. – eerorika Mar 07 '17 at 10:17
  • Yes, unqualified calls of virtual functions are *always* virtual. However, in the constructor of a class T the **dynamic type** is T. And that means that virtual calls, e.g. from base class functions, will at most go down to T's implementation of the relevant functions. – Cheers and hth. - Alf Mar 07 '17 at 10:20
  • I've posted an [SO question about the aspect that has me in doubt here](http://stackoverflow.com/questions/42645853/calling-class-ts-implementation-of-pure-virtual-from-t-constructor-without-qual). Possibly due to lack of coffeee - I'm out of coffee! But still. – Cheers and hth. - Alf Mar 07 '17 at 10:33
  • 1
    I got [verification that the behavior is indeed UB in all cases](http://stackoverflow.com/questions/42645853/calling-class-ts-implementation-of-pure-virtual-from-t-constructor-without-qual#42646077), as your answer implies. – Cheers and hth. - Alf Mar 07 '17 at 10:57
0

I have not found any compiler switch for this in visual studio, but there is a handler in the Visual Studio Runtime for this: _purecall , which is located in the runtime file PureVirt.c . It will be called every time when there is no entry in the vtable for a virtual function, which is the case when you have a pure virtual call.

You can overload this function and add a DebugBreak

But this is just a workaround to get the callstack for your pure virtual call.

KimKulling
  • 2,654
  • 1
  • 15
  • 26