This is effectively the same issue as C++ static code analysis: Polymorphic base class destructor should be either public virtual or protected non-virtual Except, when the base class doesn’t have any virtual functions to begin with, the problem can exist, that rule doesn’t find this. This must be detected at the usage. In principle, any time you manage the ownership of a derived class through a pointer to base, whether it’s by smart pointers, or raw pointers, the base class needs to have a virtual destructor for the destructor of the derived class to be called.
Base class should have a virtual destructor when derived objects are managed through pointer to base
Hi @torgeir.skogen,
While I agree with your general statement, when we implemented the rule C++ static code analysis: Polymorphic base class destructor should be either public virtual or protected non-virtual, we considered that the situation you describe was pretty rare, because the reason for managing derived instances through base class pointers was to have polymorphic behavior. So we decided not to make the rule significantly more complex for such a case.
Do you have some common use-case in mind where the base class does not contain any virtual function?
Makes sense. On closer inspection, the class that caused me to look at this does have a single virtual function. Otherwise, I agree, this is a very odd use case, that probably doesn’t need to be supported.
This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.