sdbusplus: use virtual destructor or not

Lei YU mine260309 at gmail.com
Tue Sep 17 16:52:39 AEST 2019


This email is to discuss the destructors in sdbusplus.

In current sdbusplus, some classes are meant to be inherited, but they have
non-virtual destructor.
For example, `sdbusplus::server::object::object` is usually used to composite
interfaces and let an implementation to inherit the object.

In most cases, this is fine because we usually do not write code to delete the
base class.
But in case some code is written to delete polymorphically demostrated as
below, it is undefined behavior:

```
using SomeInterfacesInherit = sdbusplus::server::object::object<SomeInterfaces>;
class Implement : public SomeInterfacesInherit
{
  ...
};

// We have undefined behavior with this!!
std::unique_ptr<SomeInterfacesInherit> foo = std::make_unique<Implement>();
```

So the question I would like to bring up is, should we make such destructors
virtual?

Reference from Herb Sutter:
> Guideline #4: A base class destructor should be either public and virtual,
> or protected and nonvirtual.

http://www.gotw.ca/publications/mill18.htm


More information about the openbmc mailing list