CWARN.DTOR.VOIDPTR

Delete expression with an object of type pointer to void

The CWARN.DTOR.VOIDPTR checker detects and reports instances of the C++ operator delete used with an operand that has the type 'pointer to void'.

A pointer to void type can be used to hide implementation details of the actual object type used. Calling delete with an operand of this type will bypass the underlying object destructor call, and can result in memory and resource leaks. Releasing memory with the delete operator called on a void pointer is therefore a suspicious practice and can indicate a bug.

Vulnerable code example

Copy
   void foo()
   {
       void* ptr = createAnObj();
       delete ptr; // deleting void pointer - bypasses destructor call
   }

In this example, Klocwork reports the expression 'delete ptr' as a CWARN.DTOR.VOIDPTR defect.

Fixed code example

Use library implementer-recommended methods to dispose of these objects, as shown in the following example.

Copy
   void foo()
   {
       void* ptr = createAnObj();
       destroyAnObj(ptr); // a sample proper way of the object disposal
   }