-2

My company uses Parasoft to validate the correctness of our c/c++ program.

In the source code, many classes are not used as base class and they don't have virtual member functions. But they inherit from other class. Here is the sample code:

class class_a : public base{
    protected:
        int* pa;

    public:
        class_a();
        ~class_a(){free(pa);};
        int* get_a(){return pa};
        ...
}

However, parosoft says:

Destructor ~class_a should be virtual

If I change the destructor to be virtual, the violation disappears.But I don't think this is the correct way to fix it.

So, Is this just false violation message or are there actually some flaws in our code?

What may cause this kind of parasoft error?

Under what conditions will parasoft show the same error message?

Follow up: Many of these classes define functions that are totally the same with their base class.These functions are non-virtual.

Yuan Wen
  • 1,583
  • 3
  • 20
  • 38
  • If it won't be used as base class, you might change `pa` from `protected` to `private`. – songyuanyao Aug 17 '16 at 02:32
  • Oh, I see.But for classes that only have private member variables,parasoft shows the same violation messages.@songyuanyao – Yuan Wen Aug 17 '16 at 02:40
  • SCA engines tend to report false positives, look for a better one may be. – πάντα ῥεῖ Aug 17 '16 at 02:44
  • What is SCA?@πάνταῥεῖ – Yuan Wen Aug 17 '16 at 02:47
  • Static Code Analysis – πάντα ῥεῖ Aug 17 '16 at 02:47
  • A non-virtual destructor in a class that is not intended to be destructed polymorphically is prefectly valid. Source : everything in the standard library. – Quentin Aug 17 '16 at 07:58
  • 1
    See parasoft help (right clic on violation, and see help on rule, you will find every thing about this violation. After that, if you think it's not relevant in your case, simply deactivate this rule). I think base class has a virtual destructor, so derived one should have also virtual destructor (just for information, what is the code of rule which report this violation ?) – Garf365 Aug 23 '16 at 12:00

2 Answers2

0

Just let the tool know the class isn't intended as a base class :

class class_a final {

The tool should know out that it's pointless to have a virtual destructor in a final class.

MSalters
  • 173,980
  • 10
  • 155
  • 350
0

The reason is rather simple. Because the base class has a virtual member function but its destructor is non-virtual.

Yuan Wen
  • 1,583
  • 3
  • 20
  • 38