1

Given this program:

struct Base
{
  virtual void f() {}
};

struct Derived:public Base
{
};

int main()
{
  Derived* c = new Derived;
  delete c;
}

gcc-4.4 -Wall is fine but gcc-5.2 -Wall gives warning: deleting object of polymorphic class type 'Derived' which has non-virtual destructor might cause undefined behaviour [-Wdelete-non-virtual-dtor]

I saw the discussion on deleting a base pointer but in my case it's the derived object. I think it's a gcc bug, but apparently GNU doesn't think so. Is there anyway to get rid of the warning without changing the base class definition?

1
  • 1
    The code is correct as-is; you could ignore the warning.
    – M.M
    Commented Jul 16, 2016 at 5:42

3 Answers 3

3

GCC has every right to emit that warning. Why? Because unless you declare that Derived is final, it's entirely possible for someone to create a MoreDerived type that is derived from Derived. At which point, your deletion of a Derived pointer can very much be invalid.

Adding a virtual destructor to Base has no real downsides. Yes, the destructor will be a virtual call. But that's hardly going to be a performance bottleneck.

13
  • Adding "final" in the Derived class would solve the issue. I can't change the Base class as it's provided by vendor. Well, I guess I can change .h but not .so. A couple of off-topic but related questions: 1. if there's no ~Base(), can I safely add "virtual ~Base() {}"? 2. If there is non-virtual dtor in Base, can I safely add virtual?
    – zhao
    Commented Jul 16, 2016 at 5:40
  • 1
    your vendor really should fix this, it is a big no-no in c++ and a common job interview question,
    – Chris Beck
    Commented Jul 16, 2016 at 5:53
  • No, the warning is wrong. The type of the pointee is Derived, regardless of whether there is a MoreDerived type, and there's no problem deleting it in the context shown. Granted, in a different context, the warning would be right, but that's not the case here. Commented Jul 16, 2016 at 12:10
  • @PeteBecker: And how would the compiler know that? Why should the compiler bother to check the over-simplified cases like this, where the object is allocated right before being destroyed? Commented Jul 16, 2016 at 13:39
  • 2
    @PeteBecker: Compilers emit warnings for code that is "just fine" all the time. That's what warnings are for: to warn you that, while the code you've written is legal C++, it may not be doing what you wanted it to. It's no different from warning on if(x = 4). Well, there is a difference in that there's a common convention for shutting the compiler up in that case. Commented Jul 16, 2016 at 15:04
2

Adding virtual ~Base() {} to Base would fix the warning.

So would adding final to Derived.

class Derived final : public Base
{
    ...
};

And so would adding a virtual destructor to Derived.

class Derived : public Base
{
public:
    virtual ~Derived() {}

    ...
};

Also, if you don't plan on using Derived polymorphically, you could inherit it privately. This doesn't prevent the warning, but you could never assign a Derived pointer to a Base pointer, and thus could never delete a Derived via a Base pointer.

class Derived : private Base
{
    ...
};
2
  • I'm aware that the code is 100% legal, works as intended, and that there is no undefined behavior. At the same time, sometimes people want to get rid of all warnings in their code. Since GCC likely won't change the warning, some other solution is needed.
    – evan
    Commented Jul 16, 2016 at 16:37
  • Yes, sorry. Perseveration from the other answer. Yours is simply a direct answer to the question, explaining how to get rid of the warning. I've deleted my comment. Commented Jul 16, 2016 at 16:47
0

The C++ Standard, [expr.delete], paragraph 3 [ISO/IEC 14882-2014], states the following:

In the first alternative (delete object), if the static type of the object to be deleted is different from its dynamic type, the static type shall be a base class of the dynamic type of the object to be deleted and the static type shall have a virtual destructor or the behavior is undefined. In the second alternative (delete array) if the dynamic type of the object to be deleted differs from its static type, the behavior is undefined.

Do not delete an object of derived class type through a pointer to its base class type that has a non-virtual destructor. Instead, the base class should be defined with a virtual destructor. Deleting an object through a pointer to a type without a virtual destructor results in undefined behavior. In this compliant solution, the destructor for Base has an explicitly declared virtual destructor, ensuring that the polymorphic delete operation results in well-defined behavior.

struct Base {
  virtual ~Base() = default;
  virtual void f();
};

struct Derived : Base {};

void f() {
  Base *b = new Derived();
  // ...
  delete b;
}

You can find more information, if use this link: https://wiki.sei.cmu.edu/confluence/display/cplusplus/OOP52-CPP.+Do+not+delete+a+polymorphic+object+without+a+virtual+destructor

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.