[Bug c++/100495] constexpr virtual destructor incorrectly reports memory leak

ldalessandro at gmail dot com gcc-bugzilla@gcc.gnu.org
Sun May 9 21:28:08 GMT 2021


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=100495

--- Comment #2 from Luke Dalessandro <ldalessandro at gmail dot com> ---
It's also possible to workaround this with array allocation.

```
struct Foo {
    constexpr virtual ~Foo() {}
};

constexpr bool foo() {
    Foo *ptr = new Foo[1]{};
    delete [] ptr;
    return true;
}

static_assert(foo());
```

Obviously both of the workarounds require that we be able to cast from a `Foo*`
to our derived pointer, at which point we might as well skip the virtual
destructor and add an abstract `virtual void destroy() = 0` member where we can
just call `delete this` anyway.


More information about the Gcc-bugs mailing list