Open
Bug 1511014
Opened 7 years ago
Updated 4 years ago
Add lint Warning/Error when an overridden virtual function is called in a destructor
Categories
(Developer Infrastructure :: Source Code Analysis, enhancement)
Developer Infrastructure
Source Code Analysis
Tracking
(Not tracked)
NEW
People
(Reporter: nical, Unassigned)
Details
It would be great to avoid calling virtual methods in the destructor of a parent class when the child class overrides that method.
Example
> class Parent {
> ...
> virtual void CleanThingsUp() { ... }
> ...
> ~Parent() {
> Reset();
> }
> ...
> void Reset() {
> // Not obvious that this calls different overloads
> // depending on whether invoked within a destructor.
> CleanThingsUp();
> }
> };
>
> class Child : public Parent {
> ...
> // Implementor happily assuming overriding CleanThingsUp
> // is enough but this overload won't run in the destructor.
> virtual void CleanThingsUp() override { ... }
> };
The above is valid C++ but in my experience it has often been a source of confusion and bugs.
The risk with this is the wrong expectation that for a given object of type Child, CleanThingsUp will always call the overloaded version, which isn't the case during constructors and destructors.
This has caused bugs in the past, some of which were caught during reviews but most of them weren't because in many cases the virtual function is called in some other function that is called in a destructor and reviewers can't systematically walk through each possible call chains to see if a virtual method can be called within a destructor each time a virtual keyword is added or any function that might be invoked within a destructor is changed, and then decide whether the intended behavior is respected.
Outright ruling out every virtual calls in constructors/destructors might be too drastic and I'm sure a lot of code relies on it for good reasons. Perhaps warning when a destructor calls a virtual method that is overloaded by child classes in the hierarchy would help catching mistakes, if only in a tool that highlight this type of error prone patterns during code reviews.
Updated•4 years ago
|
Product: Firefox Build System → Developer Infrastructure
Updated•4 years ago
|
Severity: normal → S3
You need to log in
before you can comment on or make changes to this bug.
Description
•