I occasionally get this error
pure virtual method called
terminate called without an active exception
I've added a number of try/catch blocks to the code, but is there some tool that can help trap this error ?
valgrind is not usable in my situation since this is a high speed encoder with high memory usage, so it takes forever to run.
Any tips or clues would be most welcome.
A possible cause is that you are calling a virtual member function in a constructor or destructor, where virtual function calls don't work yet. This is because:
For example:
struct Base {
Base() { foo(); } // undefined behavior
virtual void foo() = 0;
};
struct Derived : Base {
void foo() final {}
}
This is normally very easy, because compilers can typically detect this and issue a warning:
<source>:2:14: warning: call to pure virtual member function 'foo' has undefined behavior;
overrides of 'foo' in subclasses are not available in the
constructor of 'Base' [-Wcall-to-pure-virtual-from-ctor-dtor]
Base() { foo(); }
^
If this doesn't work for whatever reason, we can track down calls to Base::foo(), using a debugger and making it stop when std::terminate is called. break abort in GDB should work, because std::terminate calls std::abort by default.
If this isn't possible, we can define a terminate handler using std::set_terminate and put a breakpoint inside.
If this isn't an available option for whatever reason (e.g. C++98 code base), we can define Base::foo() and put a breakpoint inside:
void Base::foo() {
// breakpoint here
}
In GCC/on systems using the Itanium ABI, you can define the function __cxa_pure_virtual. This is the function that gets called when a pure virtual function would have gotten invoked that doesn't have another definition.
By default, it just prints "pure virtual method called" then terminates. You can make it throw an exception with a stack trace or have a break point:
extern "C" void __cxa_pure_virtual() {
trap_signal();
gdb_breakpoint();
print_stacktrace();
throw std::runtime_error("pure virtual function called");
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With