Let's consider follwing code. In fact this is narrowed problem I found using gmock and mocking void(void) method.
class Base {
public:
virtual ~Base() {}
};
class Derived : public Base
{
public:
void GetValueAndDelete() { delete this; } //here we crash
};
int main() {
Derived* p = 0;
p->GetValueAndDelete();
}
Building it with:
/tools/gcc6.1/bin/g++ --version
g++ (GCC) 6.1.0
with optimization level different than -O0 and running the result causes segmentation fault.
Is it gcc bug or something with c++ code (yes, yes, I know that it uses side effects, but it works with other compilers and without optimization as well)