I've come accross a strange issue when gcc 7.3 optimizes some code I'm using from the google test library. The issue's been fixed in google test but I'm still puzzled by the question: are there more global delete operators with the same signature for some reason?
My code is here: https://godbolt.org/z/86Eevc and what's more interesting is that with -O0 the same delete address is used with all the call instructions. It's with -O3 that everything starts getting interesting. (The link on godbolt is useful with "Compile to binary" checked so that you can see the addresses generated for the call arguments).
My class (DeleteInside) derives from a class that has a virtual table, and has a deleteMe() method that does delete this;. If I declare a DeleteInside *ptr = nullptr; at global scope and then call delete ptr; in the main function the address of the delete operator is different than the delete that is called when I use ptr->deleteMe();.
Can anyone explain?
static int timesDeleted = 1;
class DeleteBase
{
public:
virtual ~DeleteBase() {
timesDeleted += 1;
}
};
class DeleteInside
{
int _val;
public:
~DeleteInside() {
timesDeleted = (timesDeleted * _val);
}
bool doSomething() {
return false;
}
__attribute__((noinline)) void deleteMe() const {
timesDeleted += 1;
delete this;
}
};
DeleteInside *delInside = nullptr;
int main(int argc, char *argv[]) {
//delete delInside;
delInside->deleteMe();
delete delInside;
return timesDeleted;
}